php - mysql multiple keyword search in any order -
this question exact duplicate of:
i have simple mysql database keyword search functional. results search not being returned if keywords not in same order entered in database. example searching "dog cat lion" return result, searching "dog lion cat" return no results. on how fix issue appreciated. here code.
<!doctype html> <html> <head> <meta charset="utf-8"> <title>part search</title> </head> <body> <?php $username = "xxxx"; $password = "xxxx"; $hostname = "localhost"; //connection database mysql_connect($hostname, $username, $password); mysql_select_db("wesco"); $search = mysql_real_escape_string(trim($_post['searchterm'])); $find_parts = mysql_query("select * `parts` `keywords` '%$search%'"); while($row = mysql_fetch_assoc($find_parts)) { $name = $row['name']; echo "$name<br />"; } ?> </body> </html>
you use this
$searcharray = explode(" ", $_post['searchterm']); $query = ""; foreach($searcharray $val) { $search = mysql_real_escape_string(trim($val)); if (!empty($query)) { $query = $query . " or "; // or and, depends on want } $query = $query . "`keywords` '%$search%'"; } if (!empty($query)) { $find_parts = mysql_query("select * `parts` $query"); }
also, should stop using mysql_*
functions , start using object oriented implementation of mysqli.
Comments
Post a Comment