我正在尝试使用 ajax 创建实时搜索,该搜索将从 mysql 获取产品信息。请查看我在其中放置带有 ajax 代码的搜索文本字段的 index.php
<html>
<head>
<script>
function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","product.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
我不确定我是否在数据库中编写了正确的代码,请参阅代码底部,如您所见,我收到错误消息
警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在
它还说
未定义的变量:C 中的 a ...... 表示 for($i=0; $i 的这一行
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','password','table');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM product WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>product name</th>
<th>product retail price</th>
<th>product price</th>
<th>product id</th>
<th>product category</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['product_retail_price'] . "</td>";
echo "<td>" . $row['product_price'] . "</td>";
echo "<td>" . $row['product_id'] . "</td>";
echo "<td>" . $row['product_category'] . "</td>";
echo "</tr>";
}
echo "</table>";
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
mysqli_close($con);
?>
知道出了什么问题!以及如何解决这个问题!
是