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" Quote Link to comment Share on other sites More sharing options...
c_pattle Posted May 18, 2010 Author Share Posted May 18, 2010 anyone? Quote Link to comment 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'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.