duanli9591 2015-07-07 16:56
浏览 186
已采纳

SQL查询只返回1行

I am working on a php project where users can select a make and model of a car from a form and this will trigger the search for products that apply for that particular combination. When I run the query in SQL, and there are multiple products, I get multiple rows. but when I try to display those results using PHP I only get 1 row. Do you have an idea what I am doing wrong? here's the code:

$query1 = "SELECT * FROM partmakes WHERE MAKE = '$_POST[make]' and MODEL ='$_POST[model]'";
$results1 = mysqli_query($cnx, $query1);
$row1 = mysqli_fetch_assoc($results1);


$query2 = "SELECT * FROM `products` WHERE `PARTNO`= '$row1[PARTMAKE1]' OR (`PARTNO`= 
'$row1[PARTMAKE2]' OR `PARTNO`= '$row1[PARTMAKE3]' OR `PARTNO`= '$row1[PARTMAKE4]');";

$results2 = mysqli_query($cnx, $query2);

echo "These are the products for your car:<br />";
while ($row2 = mysqli_fetch_assoc($results2)){
    extract($row2);
    echo "$row2[PARTNO]<br />";
}

Thanks a lot for your help


edit - content from OP's file http://standtek.com.mx/test/screenshots.docx in the comments

So the user will select from a drop down the make and the model of the car. The first query looks for the make and model in the table: enter image description here

Then I use the information from the PARTMAKE columns as reference for the second query

Here’s an example of the query in phpmyadmin and it shows 4 rows: enter image description here

My current script displays only 1 row for some reason: enter image description here

This is what I want displayed in the results: enter image description here

  • 写回答

1条回答

  • doule0941 2015-07-07 20:17
    关注

    Use a JOIN query and you could return all the rows -

    $query = "SELECT products.* 
              FROM products
              JOIN partmakes 
              ON (
                 products.PARTNO = partmakes.PARTMAKE1 
                 OR 
                 products.PARTNO = partmakes.PARTMAKE2 
                 OR 
                 products.PARTNO = partmakes.PARTMAKE3 
                 OR
                 products.PARTNO = partmakes.PARTMAKE4) 
              WHERE partmakes.MAKE = '$_POST[make]' 
              AND partmakes.MODEL ='$_POST[model]'";
    
    $results = mysqli_query($cnx, $query);
    
    echo "These are the products for your car:<br />";
    
    echo "<table><tr><th>PARTNO</th><th>TITLE</th><th>PRICE</th></tr>";
    
    while ($row = mysqli_fetch_assoc($results)){
        echo "<tr><td>{$row[PARTNO]}</td><td>{$row[TITLE]}</td><td>{$row[PRICE]}</td></tr>";
    }
    echo "</table>";
    

    Make sure to sanitize your $_POST user data.

    You can see the JOIN query working in sqlFiddle - http://sqlfiddle.com/#!9/754feb/4

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?