我正在寻找的基本方法是使用ajax
从db中获取数据。使用Ajax从数据库中获取数据
每个人把mouseover
的类别名称,我希望这个类别的数据在div内弹出。
由于某种原因,while循环什么都不做。
这是我第一次使用ajax,也是我不是那个亲php
。
function showCat(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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","getcat.php?q="+str,true);
xmlhttp.send();
}
}
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','ipattcoi','6#uP!AR3G_','patt_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"patt_db");
$sql="SELECT * FROM products WHERE category = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['product_pic'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
这可能不是主要问题,但你有5个列定义上面的while循环,而只有4个被回声里面while循环。你是否试图打印和执行查询seperatly? – Varun
不是问题的原因,但感谢指出那个朋友 – Boiko