我正在尝试使用ajax创建实时搜索,该搜索将从mysql获取产品信息。请参阅index.php,其中我将搜索文本字段与ajax代码
放在一起<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,布尔值在
中给出并且还说
未定义的变量:a在C中...........这行说了 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);
?>
知道出了什么问题!以及如何解决这个问题!
AM
答案 0 :(得分:0)
您提到的警告通常在执行MySQL查询时出错。所以,
而不是
$result = mysqli_query($con,$sql);
使用
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
这将帮助您找出MySQL中发生的错误。
或者,你可以在phpMyAdmin&gt;中运行查询的副本。 db_name&gt; sql直接
可能是任何事情,也许你的表名错了。