我创建了一个用于搜索MySQL数据库的php搜索脚本,但没有搜索返回结果。
表格代码
<form method="post" action="search.php">
<input type="text" value="Search..." name="query" />
<input type="submit" value="Find" name="completedsearch" />
</form>
PHP脚本
<?php
if(isset($_POST['completedsearch']))
{
$term = $_POST['query'];
$mysql = mysql_connect("localhost","searchuser","password");
mysql_select_db("hcsd");
$qu = mysql_query("SELECT * FROM your_table WHERE COMPANY LIKE '%{$term}%' OR LOCATION LIKE '%{$term}%' OR KEYWORDS LIKE '%{$term}%' OR PRODUCTSSERVICES LIKE '%{$term}%' ");
echo "
<th>Name</th>
<th>Location</th>
<th>Products/Services</th>
";
while($row = mysql_fetch_array($qu))
{
echo "<tr><td>";
echo $row['COMPANY'];
echo "</td>";
echo "<td>";
echo $row['LOCATION'];
echo "</td>";
echo "<td>";
echo $row['PRODUCTSSERVICES'];
echo "</tr></td>";
}
}
?>
MySQL数据库有4列,标题为COMPANY,LOCATION,KEYWORDS&amp; PRODUCTSSERVICES,这个脚本应该搜索搜索词的任何列,然后显示表中任何匹配行的COMPANY,LOCATION和PRODUCTSSERVICES,甚至使用搜索词我知道100%在MySQL表中,我是仍未收到任何结果。
答案 0 :(得分:1)
我不确定花括号在这里有帮助......
替换
$qu = mysql_query("SELECT * FROM your_table WHERE COMPANY LIKE '%{$term}%' OR LOCATION LIKE '%{$term}%' OR KEYWORDS LIKE '%{$term}%' OR PRODUCTSSERVICES LIKE '%{$term}%' ");
使用
$qu = mysql_query("SELECT * FROM your_table WHERE COMPANY LIKE '%".$term."%' OR LOCATION LIKE '%".$term."%' OR KEYWORDS LIKE '%".$term."%' OR PRODUCTSSERVICES LIKE '%".$term."%'");
您也应该转义输入
$qu = mysql_query("SELECT * FROM your_table WHERE COMPANY LIKE '%".mysql_real_escape_string($term)."%' OR LOCATION LIKE '%".mysql_real_escape_string($term)."%' OR KEYWORDS LIKE '%".mysql_real_escape_string($term)."%' OR PRODUCTSSERVICES LIKE '%".mysql_real_escape_string($term)."%'");
还要考虑使用更新的mysqli_ functions
答案 1 :(得分:1)
你的桌子叫什么名字?
您应该在phpmyadmin或用于管理数据库的任何软件中尝试您的请求并查看结果。如果不是结果,则您的请求中可能存在错误。