我正在尝试提出一个查询,该查询将根据数据库中包含的字符串的字母模式生成结果。
基本上这就是我想要做的事情:
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
这是我到目前为止所尝试的($ searchword对应于测试用例字符串):
$results = mysql_query("SELECT * FROM products WHERE prod_name LIKE %".$searchword."%");
答案 0 :(得分:3)
将引号('%text%"
)添加到您的查询
$results = mysql_query("SELECT * FROM products
WHERE prod_name LIKE '%".$searchword."%'");
searchword
'开头的话,他就是对的:
$results = mysql_query("SELECT * FROM products
WHERE prod_name LIKE '".$searchword."%'")
即:
如果searchword
= TOY,此查询WHERE prod_name LIKE '".$searchword."%'
将显示TOYS,TOYZ,TOYXXX等,但不会显示XTOY,TITOY,ZTOY等。
如果您想了解有关如何使用like
的更多信息,请点击此处:
http://dev.mysql.com/doc/refman/5.1/en/string-comparison-functions.html