Forum Moderators: coopster
$searchstring = red blue black
The PHP processes it like this (searching multiple fields of one table):
$sql = "SELECT * FROM table WHERE field1 LIKE '%$searchstring%' OR field2 LIKE '%$searchstring%' OR field3 LIKE '%$searchstring%'";
Even though all these words exist in the database - albeit each one in a separate field - no results are returned.
I think the problem lies in the fact that the script is looking for the "red blue black" together in one place. I think I need to split the string, store in an array and have the script search each word separately.
But I have no idea how to do this. Can anyone help?
Many thanks,
Maynard.
$searcharray = Array();
$searcharray = explode(" ",$searchstring);
$sql = "SELECT * FROM table WHERE 1=1 ";
reset($searcharray);
while (list($key,$val) = each($searcharray)
{
$sql .= " AND (field1 LIKE '%$val%' OR field2 LIKE '%$val%' OR field3 LIKE '%$val%')";
}
The above should break apart your string string based on spaces, and then search for a a match in the database where each word is contained in a field.
Is this what you're after?
HTH,
JP
Your code does not display duplicates?
JP, thanks for being so helpful. As I love learning PHP, could you explain - line by line - what your code is doing (if you have time)?
Eg, what does "WHERE 1=1" mean, what is "reset($searcharray)" doing; what does this mean "while (list($key,$val) = each($searcharray)"; and this ".="?
$searcharray = Array();
$searcharray = explode(" ",$searchstring);
$sql = "SELECT * FROM table WHERE 1=1 ";
reset($searcharray);
while (list($key,$val) = each($searcharray)
{
$sql .= " AND (field1 LIKE '%$val%' OR field2 LIKE '%$val%' OR field3 LIKE '%$val%')";
}
[b]// define variable as array[/b]
$searcharray = Array(); [b]// split up input string based on space character and store in array[/b]
$searcharray = explode(" ",$searchstring);
[b]// start sql statement. 1=1 is there so that we can use AND at the start of every iteration of the while loop. Not very clean, but quicker to code :)[/b]
$sql = "SELECT * FROM table WHERE 1=1 ";
[b]// ensure the pointer is back at the start of the array, or else each will not work properly.[/b]
reset($searcharray);
[b]// step through each element of searcharray, using $key to hold the key reference of the array, and $val to hold the actual value of the current key.[/b]
while (list($key,$val) = each($searcharray)
{
[b]// .= appends the string in " " to the contents of variable $sql. Just having = would overwrite $sql.[/b]
$sql .= " AND (field1 LIKE '%$val%' OR field2 LIKE '%$val%' OR field3 LIKE '%$val%')";
}
SELECT * FROM table WHERE field1~*'(key1Škey2Škey3)' OR field2~*'(key1Škey2Škey3)';
Not sure if there's an equivalent for MySQL but someone here will know...
;)
How would I include an option to "search any words", based on the code you provided?
Bear in mind that the query is going to be get rather slow as your search table holds more information. In an ideal world you don't want to use like %WORD% as this means MySQL has to search through every single word of each field you're looking at, and this takes *lots* of time!
If its' for a small-ish web site, this shouldn't matter too much. Just don't expect Google-like responsiveness using this sort of system when using masses of data! :)
JP