Jump to content

PHP Search


c_pattle

Recommended Posts

I've just made a search form on my website where the user can search for a product and I have used the following code

 

$search_query = 'select * from product_list where product_name="' . $search_word . '"';

 

however this only works if the user types the exact name of the product.  Is there a mysql select I can do where it will find all product name which feature part of the search word.  For example if the users searches for "frying pan" it will find all products with the word "frying" somewhere in their name and not just the product with the exact name "frying pan"

Link to comment
Share on other sites

You need to use LIKE and the wildcard %.

 

$search_word."%" would look for all product which begin with what was inputted whereas "%".$search_word."%" would search for all products that contain the search word.

 

You must be careful about SQL injection however - have a read of http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

 

$search_word = mysql_real_escape_string($search_word):
$search_word = $search_word."%";  //or $search_word = "%".$search_word."%"; 
$search_query = 'select * from product_list where product_name like '$search_word';

 

 

Link to comment
Share on other sites

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.