如何在php中从一个数据库的两个不同表中获取多个数据

pwuypxnk  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(453)

我正在创建购物网站,现在我有一个表调用产品,所有的产品数据都存储在其中,我有不同的表购物车,其中所有的产品都被添加到购物车中,所以基本上在购物车中我存储了productid,userid所以现在在checkout页面上,我想从cart表中检索数据,我们将获取特定登录用户的productid,从表products中我们将显示添加到cart的所有产品,因此基本上从cart表中我们将获取添加到cart的产品,从表products中我们将显示它们。现在的问题是,我检索到的数据,但它只显示一个项目,而不是多个。提前谢谢
这是我的密码:

  1. <?php $userid = $_SESSION['userSession'];
  2. require_once 'dbconfig.php';
  3. $stmt = $DB_con->prepare('SELECT cartid,userid,productid FROM cart WHERE userid =:uid');
  4. $stmt->execute(array(':uid'=>$userid));
  5. if($stmt->rowCount() > 0)
  6. {
  7. while($rows=$stmt->fetch(PDO::FETCH_ASSOC))
  8. {
  9. extract($rows);
  10. {
  11. }
  12. ?>
  13. <?php
  14. $productid = $rows['productid'];
  15. require_once 'dbconfig.php';
  16. $stmt = $DB_con->prepare('SELECT productid,productname,productcategory,producttype,productprice,productimage1 FROM products WHERE productid = :uid');
  17. $stmt->execute(array(':uid'=>$productid));
  18. if($stmt->rowCount() > 0)
  19. {
  20. while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  21. {
  22. extract($row);
  23. {
  24. }
  25. ?>
  26. <div class="bs-example4" data-example-id="simple-responsive-table">
  27. <div class="table-responsive">
  28. <table class="table-heading simpleCart_shelfItem">
  29. <tr>
  30. <th class="table-grid">Item</th>
  31. <th>Prices<h3></h3></th>
  32. <th >Delivery </th>
  33. <th>Subtotal</th>
  34. </tr>
  35. <tr class="cart-header1">
  36. <td class="ring-in"><a href="single.html" class="at-in"><img src="thumb/<?php echo $row['productimage1']; ?>" class="img-responsive" alt=""></a>
  37. <div class="sed">
  38. <h5><a href="single.html"><?php echo $row['productname']; ?></a></h5>
  39. </div>
  40. <div class="clearfix"> </div>
  41. <div class="close2"> </div></td>
  42. <td>&#x20b9; <?php echo $row['productprice']; ?></td>
  43. <td>FREE SHIPPING</td>
  44. <td class="item_price">$100.00</td>
  45. <td class="add-check"><a class="item_add hvr-skew-backward" href="#">Add To Cart</a></td>
  46. </tr>
  47. </table>
  48. </div>
  49. </div>
  50. <div class="produced">
  51. <a href="single.html" class="hvr-skew-backward">Produced To Buy</a>
  52. </div>
  53. </div>
  54. </div>
  55. <?php
  56. }
  57. }
  58. else
  59. {
  60. ?>
  61. <div class="col-xs-12">
  62. <div class="alert alert-warning">
  63. <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
  64. </div>
  65. </div>
  66. <?php
  67. }
  68. ?> <?php
  69. }
  70. }
  71. else
  72. {
  73. ?>
  74. <div class="col-xs-12">
  75. <div class="alert alert-warning">
  76. <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
  77. </div>
  78. </div>
  79. <?php
  80. }
  81. ?>
z9gpfhce

z9gpfhce1#

我要加上这个技巧,它更接近于我所用的,没有其他人做得完全一样。

  1. select *
  2. from cart c,
  3. products p
  4. where p.product_id = c.product_id
  5. and c.userid = :uid
sdnqo3pr

sdnqo3pr2#

可以使用一个使用内部联接的查询

  1. SELECT a.cartid
  2. , a.userid
  3. , a.productid
  4. , b.productname
  5. , b.productcategory
  6. , b.producttype
  7. , b.productprice
  8. , b.productimage1
  9. FROM cart
  10. INNER JOIN products ON a.productid = b.productid
  11. where a.productid= :uid
2j4z5cfb

2j4z5cfb3#

您应该对join语句使用另一个且只能使用一个查询。

  1. SELECT * FROM cart c JOIN products p ON p.productid = c.productid WHERE c.userid = :uid

您将收到的数据将包含来自购物车的产品数据。

相关问题