简体   繁体   English

使用php从数据库中获取多行

[英]Fetching multiple rows from Database using php

I am building a bit of a filter function that fetches items meeting certain criteria. 我正在构建一些过滤器功能,以获取满足某些条件的项目。 I'm using AJAX, and a bit of basic php, I search for items which match a certain ID. 我正在使用AJAX,还有一些基本的php,我会搜索与某个ID匹配的项目。 My problem is, as soon as there are rows that have duplicate id's, nothing is displayed. 我的问题是,一旦有重复ID的行,什么都不会显示。 I want all rows with the matching ID to be displayed. 我希望显示所有具有匹配ID的行。 I cannot seem to see a duplicate of this question, however if there is a solution out there I would be most grateful. 我似乎看不到这个问题的重复,但是,如果有解决的办法,我将不胜感激。 I have presented my code below: 我在下面介绍了我的代码:

My filter.php: 我的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>

And this is the front side of things: 这是事情的正面:

    <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>

Which is supported by the following script: 以下脚本支持:

<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>

If your id field in the database is an integer field, then your query should be 如果数据库中的id字段是整数字段,则您的查询应为

$search="SELECT * FROM table WHERE id = ".$q." ";

I removed the single quotes around the id variable. 我删除了id变量周围的单引号。

Try running the query in phpmyadmin or console and see what it returns. 尝试在phpmyadmin或控制台中运行查询,并查看返回的内容。

Try: 尝试:

1) Display errors: 1)显示错误:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

from: How do I get PHP errors to display? 来自: 如何获取显示的PHP错误?

2) Echo the query and try it directly in phpmyadmin to see if you get an error. 2)回显查询,然后直接在phpmyadmin中尝试,以查看是否收到错误。

echo $search; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM