我正在尝试学习如何在我的网站上放置一个有用的搜索功能,用户可以在其网站上添加某些标签。标签位于keyword
列中,该列将数据保存为逗号分隔的字符串(即red, car, apple
),(理论上)可以在以后搜索。
当我像这样构建我的SQL搜索时:
$sql = "SELECT * FROM reporting WHERE (keywords LIKE '%" . $search. "%' )
AND user_id = $user_id
ORDER BY spc_class";
//-run the query against the mysql query function
$result = mysqli_query($conn, $sql);
//-create while loop and loop through result set
$colNames = array();
$data = array();
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
$colNames = array_keys(reset($data));
} else {
echo "0 results";
}
它工作得相当好,除非有人搜索多个单词。这些问题已在这些论坛中得到广泛讨论,似乎大多数答案都建议将要搜索的列转换为mysql中的full text value
格式。
我已经这样做了,但当我像这样更改我的搜索查询时,我在使用MATCH AGAINST
构造时遇到了非对象发现错误:
$sql = "SELECT * FROM reporting WHERE MATCH(keywords) AGAINST('".$search."')
AND user_id = $user_id
ORDER BY spc_class";
//-run the query against the mysql query function
$result = mysqli_query($conn, $sql);
//-create while loop and loop through result set
$colNames = array();
$data = array();
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
$colNames = array_keys(reset($data));
} else {
echo "0 results";
}
为什么我能够使用WHERE LIKE
构造执行mysql搜索,而不是MATCH AGAINST
查询?后者不会返回一个对象吗?我似乎无法弄清楚使用全文搜索查询时的错误。