[英]Fetching multiple rows from Database using php
我正在構建一些過濾器功能,以獲取滿足某些條件的項目。 我正在使用AJAX,還有一些基本的php,我會搜索與某個ID匹配的項目。 我的問題是,一旦有重復ID的行,什么都不會顯示。 我希望顯示所有具有匹配ID的行。 我似乎看不到這個問題的重復,但是,如果有解決的辦法,我將不勝感激。 我在下面介紹了我的代碼:
我的filter.php:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
include('dbconnect.php');
$q = intval($_GET['q']);
mysqli_select_db($conn,"database");
$search="SELECT * FROM table WHERE id = '".$q."'";
$result = mysqli_query($conn,$search);
echo "<table>
<tr>
<th>Merchant</th>
<th>Product</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['merchant'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
這是事情的正面:
<div>
<ul>
<li><a href="#" class="header">Option 1</a></li>
<li onclick="showProduct(this.value)" value="1"><a>Product1a</a></li>
<li onclick="showProduct(this.value)" value="2"><a>Product 2a</a></li>
<li><a href="#" class="header">Option 2</a></li>
<li onclick="showProduct(this.value)" value="1"><a>Product 1b</a></li>
<li onclick="showProduct(this.value)" value="2"><a>Product 2b</a></li>
</ul>
</div>
<!--SEARCH OUTPUT-->
<div id="products">></div>
以下腳本支持:
<script>
function showProduct(str) {
if (str=="") {
document.getElementById("products").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 (this.readyState==4 && this.status==200) {
document.getElementById("products").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","filter.php?q="+str,true);
xmlhttp.send();
}
</script>
如果數據庫中的id字段是整數字段,則您的查詢應為
$search="SELECT * FROM table WHERE id = ".$q." ";
我刪除了id變量周圍的單引號。
嘗試在phpmyadmin或控制台中運行查詢,並查看返回的內容。
嘗試:
1)顯示錯誤:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
來自: 如何獲取顯示的PHP錯誤?
2)回顯查詢,然后直接在phpmyadmin中嘗試,以查看是否收到錯誤。
echo $search;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.