# Searching the database for consecutive words

**URL:** <https://www.phphelp.com/t/searching-the-database-for-consecutive-words/10259>\
**Category:** MySQL database\
**Created:** [August 28, 2007, 10:17pm UTC](https://www.phphelp.com/t/searching-the-database-for-consecutive-words/10259 "2007-08-28T22:17:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blaster\_god](https://www.phphelp.com/user_avatar/www.phphelp.com/blaster_god/32/63_2.png) [@blaster\_god](https://www.phphelp.com/u/blaster_god)\
**Post date:** [August 28, 2007, 10:17pm UTC](https://www.phphelp.com/t/searching-the-database-for-consecutive-words/10259/1 "2007-08-28T22:17:06Z")

</div>

Hello, I posted this message in to a few forums but no one could help me . Maybe I will get lucky here . I want to make a search in my PHP database and return all rows that contain some keywords, one after another, in any order (or at least half of them, one after another) .  
I want also to take words that has the key in them .  
If we have the word “good” , the word “goodness” will be ok and also “nogood” will be ok .

I will explain more below .  
I have an array with keywords (the array can have maximum 6 elements) . So :  
key[1]=“hello”  
key[2]=“how”  
key[3]=“snow”  
key[4]=“dancing”  
key[5]=“golf”

I need a script that will search and return all rows that contain at least 3 (5/2 majored to the largest is 3) of this keywords, one after another, in any order . So something like all combinations :

Here hello how snow is fine -\> good  
When dancing gold snow how is it ? -\> good  
how snow dancing golf is at the mountain . -\> good  
I am wondering how snowing dancing golf is. -\> good  
Fred says hellow snow -\> not good (only 2 words)

So far I am using :

```auto
$mysql_str='%';
for($i=0;$i<$no;$i++)
	$mysql_str=$mysql_str.'%'.$keyword[$i];
$mysql_str=$mysql_str.'%';
```

-\>this is to add all words in a string for the mysql query

```auto
$result=mysql_query("SELECT *FROM `articole2` WHERE `continut` LIKE '$mysql_str' ORDER BY RAND() LIMIT 1001;");
```

-\>this is to return the results .

The problem is that my script returns results that have all words, anywhere in the body of the row . I need it to work only in consecutive order . Is it possible ?  
I know this is hard … but maybe someone can help . Thank you in advance

---

<div class="post-metadata">

**Author:** ![Q1712](https://www.phphelp.com/letter_avatar_proxy/v4/letter/q/a183cd/32.png) [@Q1712](https://www.phphelp.com/u/Q1712)\
**Post date:** [September 5, 2007, 12:47am UTC](https://www.phphelp.com/t/searching-the-database-for-consecutive-words/10259/2 "2007-09-05T00:47:53Z")

</div>

sry havn’t seen ur post earlyer

u have to search for each word on it’s own, but instead of using add u may add the values (as i know MySQL is converting true to 1 automaticly, havn’t tryed)

[php]  
if($no) $mysql\_str=’ (`continut` LIKE “%’.$keyword[0].’%”)’;  
for($i=1;$i\<$no;$i++)  
$mysql\_str.=’ + (`continut` LIKE “%’.$keyword[$i].’%”)’;  
$mysql\_str.=’ \>= '.ceil($no/2);  
[/php]

[php]  
$result=mysql\_query(“SELECT \*FROM `articole2` WHERE $mysql\_str ORDER BY RAND() LIMIT 1001;”);  
[/php]

this should create something like

```auto
SELECT *FROM `articole2` WHERE + (`continut` LIKE "%hello%") + (`continut` LIKE "%how%") + (`continut` LIKE "%snow%") + (`continut` LIKE "%dancing%") + (`continut` LIKE "%golf%") >= 3 ORDER BY RAND() LIMIT 1001;
```

i don’t know if this will work, but i hope it’s pointing u in the right direction
