下面的查询将用于搜索脚本。出于某种原因,它不会返回任一条件为真的所有结果。我究竟做错了什么?
$sql = "SELECT name, id_code from codes WHERE name LIKE '%$q%' OR id_code
LIKE '%$q%'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("<div id=norequests>No results for <strong>$q</strong></div>");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$name = htmlspecialchars($row['name']);
$code = htmlspecialchars($row['id_code']);
}
print("$code: $name<br /> <br />");
}
}
else{
echo '<div id="error">No results for $q.</div>';
}