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
https://forums.phpfreaks.com/topic/202167-php-search/
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
https://forums.phpfreaks.com/topic/202167-php-search/#findComment-1060179
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.