I am trying to come up with a query that will generate results based on letter patterns of a string contained in the database.
Basically this is what I am trying to do:
Test 1: "T" => mysql returns: toys, tofu, telephones, turkeys, televisions, topaz
Test 2: "To" => mysql returns: toys, tofu, topaz
Test 3: "Toy" => mysql returns: toys
Test 4: "Toys" => mysql returns: toys
This is what i tried so far ($searchword corresponds to test case string):
$results = mysql_query("SELECT * FROM products WHERE prod_name LIKE %".$searchword."%");
Add the quotes (
'%text%") to your query@KaiQing comments that you could use this instead,and he’s right if you just want the words that start with the ‘
searchword‘:I.E:
if
searchword=TOY, this queryWHERE prod_name LIKE '".$searchword."%'will display TOYS,TOYZ,TOYXXX, etc but wont display XTOY, TITOY,ZTOY, etc.If you want to find out more about how to use
liketake a look here:http://dev.mysql.com/doc/refman/5.1/en/string-comparison-functions.html