Search All Words Of A String In mySQL

Submitted by:Jhon Brain

Date added:08 January, 2015


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: search string , search words

Code Snippet:


$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";