Page 1 of 1

PHP MySQL Clause IN() with array

Posted: Thu Dec 23, 2010 1:15 pm
by ianhull
Hi all,

I am trying to make my site search easier for usrs to find what they want.

at the moment I am using "%LIKE% clause" it works ok but I have noticed that if people search for something like "mountain bikes" it will not retunr a item which contains bikes which I would like it to do.

I did change my code to

Code: Select all


$q = explode($_REQUEST['q']);

//and in my query i used the IN($q) clause instead of %LIKE%

but I am not getting any results becuase it is not looking through the array of $q and is simply searching using $q[0]

Is there an efficient way to achieve what I want?

Thanks in advance.

Just had a thought.

I am thinking maybe if I build a query of %LIKE%'s OR etc this would work.

Code: Select all


$like_q = '';

foreach($q as $k=>$v){

$like_q .= ' OR LIKE $v ';

}//
Any suggestions?

**SOLVED** PHP MySQL Clause IN() with array

Posted: Thu Dec 23, 2010 3:41 pm
by ianhull

Code: Select all


$tag_q = explode(' ',$tag);
			$q_a = "";
			
			foreach($tag_q as $k=>$v){
				
				if($k > 0){
					
					$q_a .= " OR itemName LIKE '%$v%' ";
					
				}else{
					
					$q_a .= " AND itemName LIKE '%$v%' ";
					
				}//
				
			}//


Re: PHP MySQL Clause IN() with array

Posted: Thu Dec 23, 2010 7:41 pm
by requinix
Use FULLTEXT indexes and MATCH AGAINST. Much more powerful than just a LIKE or IN.