Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql split search string by space and push into query

Tags:

php

mysql

The search query is:

"product1 prod2 prod4"

I need to make the mysql

SELECT * 
  FROM tableprod 
  WHERE (prod LIKE '%product1%' 
         AND prod LIKE '%prod2%' 
         AND prod LIKE '%prod4%')

using the mysql_real_escape_string for the input query...

like image 330
ronaktal Avatar asked May 23 '26 21:05

ronaktal


1 Answers

Simple string manipulation:

$terms = explode(' ', $search);

$bits = array();
foreach ($terms as $term) {
    $bits[] = "prod LIKE '%".mysql_real_escape_string($term)."%'";
}

$query = "SELECT * FROM tableprod WHERE (".implode(' AND ', $bits).")";
like image 183
Tim Fountain Avatar answered May 26 '26 11:05

Tim Fountain