如何使用此代码自动提示避免 MYSQL 重复返回数据? 非常感谢你的帮助
<?php
include('conn.php');
$str = strtolower($_GET['content']);
if(strlen($str))
{
$sel = mysql_query("select * from Streams where title like '".trim($str)."%'");
if(mysql_num_rows($sel))
{
echo "<table border =\"0\" width=\"100%\">\n";
if(mysql_num_rows($sel))
{
echo "<script language=\"javascript\">box('1');</script>";
while($row = mysql_fetch_array($sel))
{
$country = str_ireplace($str,"<b>".$str."</b>",($row['title']));
echo "<tr id=\"word".$row['title']."\" onmouseover=\"highlight(1,'".$row['title']."');\" onmouseout=\"highlight(0,'".$row['title']."');\" onClick=\"display('".$row['title']."');\" >\n<td>".$country."</td>\n</tr>\n";
}
}
echo "</table>";
}
}
else
{
echo "<script language=\"javascript\">box('0');</script>";
}
?>
最佳答案
我在您的代码中看到的是您在 PHP 代码中使用的 SQL 结果集中只有一个字段 - title
。那么为什么不将您的查询写成:
"SELECT DISTINCT `title`
FROM Streams
WHERE title like '".trim($str)."%'"
或者,如果您无法更改查询,您可以将 title
存储在 PHP 数组中,然后在其上运行 array_unique
以避免在将它们写入之前重复HTML。
关于php - 如何在自动建议中避免与 MySQl 重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6398212/