• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql query "where like array of value" - need help

Can someone help me to understand the syntax for this mySql query

$matchesArray = array('love','like','happy');
$query = "SELECT * FROM column where content like '%$matchesArray%' ";

I should have mentioned this does NOT work, obviously and I need help to know how to do a query like that.

[eluser]Pascal Kriete[/eluser]
You can chain LIKE clauses using AND and OR. In this case OR is probably more appropriate:
$field = 'content';
$matches = array(‘love’,‘like’,‘happy’);

// TODO: check if matches has content

$first = array_pop($matches);
$query = "WHERE {$field} LIKE '%{$first}%' ";

foreach($matches as $match)
     $query .= "OR {$field} LIKE '%{$match}%' ";

You may want to consider looking at the active record or_like function. If you don't use active record, remember to sanitize the matches array.


i would go for the AR where_in method

ah, thats rad.

ah very cool!

[eluser]Jon L[/eluser]
This may no longer be true, but I read a year or two ago that the sql IN statement is often slower than a (this LIKE that OR this2 LIKE that2...). You may want to check to see if that's still true.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.