Jump to content

Recommended Posts

I have a form with one input where people can type in search terms.  It then takes them to a script where it assigns a value to the search term and  then runs it against a mysql product database.  The problem is I would like to break the users search query into individual words for better search results.  I have tried using explode() and it wont work.  I have no idea why, this is the select statement I am using now including all the fields which need to be searched.  Can you see any problems with it, maybe the problem isnt with the explode function but with my statement?  If not, is there a simple alternative way to doing a search against a mysql db and breaking up a query into individual keywords?  Thank you for the help.

 

$rs = mysql_query("SELECT * FROM inventory WHERE condition = 'used' AND make LIKE '%" . $keywords . "%' OR type LIKE '%" . $keywords . "%' OR item_number LIKE '%" . $keywords . "%' OR year LIKE '%" . $keywords . "%' OR size LIKE '%" . $keywords . "%' OR finish LIKE '%" . $keywords . "%' OR model LIKE '%" . $keywords . "%' OR serial_number LIKE '%" . $keywords . "%' OR price LIKE '%" . $keywords . "%' OR information LIKE '%" . $keywords . "%' OR description LIKE '%" . $keywords . "%'");

Link to comment
https://forums.phpfreaks.com/topic/66193-problem-with-mysql-search-script/
Share on other sites

The query looks ok, but i guess it will be a stress of a query for mysql server, as it has smth like 10 ORs. Anyway a normal query should be like:

 

$rs = mysql_query("SELECT * FROM inventory WHERE condition='used' AND make LIKE '%$keywords%'");

 

I just wrote it in one string without concatinating. About the explode thing:

 

$words = explode(' ', $keywords);
for($i = 0; $i < count($words); $i++){
     if($i == 0){
        $keyw = "'" . $words[$i] . "'";
     } else{
        $keyw .= " OR column='{$words[$i]}'";
     }
}
$query = "SELECT * FROM table WHERE column LIKE " . $keyw;

 

Didnt try it but guess it makes sense. It explodes the $keywords by ' ' (space) and then makes a variable $keyw which in the end will look like: "'word1' OR 'word2' OR 'word3'".

 

About a better way to search u can try the Full-Text Indexing capabilities of mysql and theres a tutorial about that in phpfreaks tutorials.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.