PHP MySQL Clause IN() with array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

PHP MySQL Clause IN() with array

Post 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?
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

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

Post 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%' ";
					
				}//
				
			}//

User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP MySQL Clause IN() with array

Post by requinix »

Use FULLTEXT indexes and MATCH AGAINST. Much more powerful than just a LIKE or IN.
Post Reply