Search All Words Of A String In mySQL

Submitted by:David Villa

Date added:03 December, 2014

Category:PHP

This code will break a string of words up along the spaces and do a mySQL search for each of the words in the string.

Tags: string , search words

Code Snippet:

<?PHP

$string = "A sample string with several words";

$words = EXPLODE(" ",$string);

FOR($i=0;$i<count($words);$i++){$query_string .= "LIKE '%".$words[$i]."%' OR ";}

$query_string = SUBSTR($query_string,0,STRLEN($query_string)-4);

$sql = "SELECT * FROM test WHERE title ".$query_string." ORDER BY id DESC";

?>
 
 

Comments