c_pattle Posted May 18, 2010 Share Posted May 18, 2010 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 More sharing options...
c_pattle Posted May 18, 2010 Author Share Posted May 18, 2010 anyone? Link to comment https://forums.phpfreaks.com/topic/202167-php-search/#findComment-1060175 Share on other sites More sharing options...
siric Posted May 18, 2010 Share Posted May 18, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.