开头的搜索关键字记录将首先出现。无法正常搜索,请帮帮我......
$terms =explode(" ", $search);
$query = "SELECT * FROM search where ";
$i = 0;
foreach ($terms as $each)
{
$i++;
if ($i == 1)
$query .= "(`title` LIKE '%".$each."%') OR (`website` LIKE '%".$each."%')";
else
$query .= "OR (`title` LIKE '%".$each."%') OR (`website` LIKE '%".$each."%')";
}
答案 0 :(得分:0)
您应该查看MySQL的Full Text Search来实现搜索功能,因为它比LIKE
运算符更灵活,更高效。
您的表定义看起来像。
CREATE TABLE search (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(200),
website TEXT,
FULLTEXT (title,website)
) ENGINE=InnoDB;
然后你的查询会是这样的。
SELECT * FROM search
WHERE MATCH (title,website)
AGAINST ('this is your query' IN NATURAL LANGUAGE MODE)
我还要确保输入已正确转义以阻止SQL Injection Attacks。