简体   繁体   English

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

[英]how to get multiple data from two different table from one database in php

I am creating shopping website now i have one table call products in which all the products data is stored and i have different table cart in which all the product which are added to cart are stored so basically in cart I have stored productid, userid so now on page called checkout I want to retrieve data in which the data from cart table we will take productid of particular logged in user and from the table products we will show all the product added to cart so basically from cart table we will take the product added to cart and from table products we will show them . 我现在正在创建购物网站,我有一个表调用产品,其中存储了所有产品数据,并且我有不同的表购物车,其中所有添加到购物车的产品都存储在其中,因此基本上在购物车中,我已经存储了productid,userid在名为checkout的页面上,我要检索数据,其中购物车表中的数据将采用特定登录用户的productid,表产品中的数据将显示添加到购物车中的所有产品,因此基本上从购物车表中我们将采用添加的产品从餐桌产品到购物车,我们将向他们展示。 now the problem is that i have retrieved the data but it only showing one item and not multiple. 现在的问题是我已经检索了数据,但它仅显示一项而不是多项。 Thanks in advance 提前致谢

Here is my code: 这是我的代码:

 <?php $userid = $_SESSION['userSession']; require_once 'dbconfig.php'; $stmt = $DB_con->prepare('SELECT cartid,userid,productid FROM cart WHERE userid =:uid'); $stmt->execute(array(':uid'=>$userid)); if($stmt->rowCount() > 0) { while($rows=$stmt->fetch(PDO::FETCH_ASSOC)) { extract($rows); { } ?> <?php $productid = $rows['productid']; require_once 'dbconfig.php'; $stmt = $DB_con->prepare('SELECT productid,productname,productcategory,producttype,productprice,productimage1 FROM products WHERE productid = :uid'); $stmt->execute(array(':uid'=>$productid)); if($stmt->rowCount() > 0) { while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { extract($row); { } ?> <div class="bs-example4" data-example-id="simple-responsive-table"> <div class="table-responsive"> <table class="table-heading simpleCart_shelfItem"> <tr> <th class="table-grid">Item</th> <th>Prices<h3></h3></th> <th >Delivery </th> <th>Subtotal</th> </tr> <tr class="cart-header1"> <td class="ring-in"><a href="single.html" class="at-in"><img src="thumb/<?php echo $row['productimage1']; ?>" class="img-responsive" alt=""></a> <div class="sed"> <h5><a href="single.html"><?php echo $row['productname']; ?></a></h5> </div> <div class="clearfix"> </div> <div class="close2"> </div></td> <td>&#x20b9; <?php echo $row['productprice']; ?></td> <td>FREE SHIPPING</td> <td class="item_price">$100.00</td> <td class="add-check"><a class="item_add hvr-skew-backward" href="#">Add To Cart</a></td> </tr> </table> </div> </div> <div class="produced"> <a href="single.html" class="hvr-skew-backward">Produced To Buy</a> </div> </div> </div> <?php } } else { ?> <div class="col-xs-12"> <div class="alert alert-warning"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ... </div> </div> <?php } ?> <?php } } else { ?> <div class="col-xs-12"> <div class="alert alert-warning"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ... </div> </div> <?php } ?> 

you could use a single query using a inner join 您可以使用内部联接使用单个查询

  SELECT  a.cartid
    , a.userid
    , a.productid
    , b.productname
    , b.productcategory
    , b.producttype
    , b.productprice
    , b.productimage1
    FROM  cart
    INNER JOIN  products ON a.productid = b.productid 
    where a.productid= :uid

You should use another and only one query with JOIN statement. 您应该对JOIN语句使用另一个且只有一个查询。

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

The data you will receive will contains products data from cart. 您将收到的数据将包含购物车中的产品数据。

I'll add this technique, it is closer to what I use and no one else did it quite the same. 我将添加这项技术,它与我使用的技术更接近,没有其他人做得完全相同。

select  *
from    cart c,
        products p
where   p.product_id = c.product_id
and     c.userid = :uid

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

相关问题 从两个不同的表中获取和联接两个数据,用于If条件PHP - GET AND JOIN two data from two different table for If conditions PHP 如何从数据库的两个表中获取数据并显示在一个html表中 - how to get data from two tables from database and display in one html table 如何使用PHP中的一个mySQL数据库表处理两组大型数据? - How to work with two large sets of data from one mySQL database table in PHP? 如何从具有不同列的两个不同表中获取数据 - How to get data from two different table with different columns 如何从 php laravel 中的两个表中获取多个值? - how to get multiple values from two table in php laravel in a table? 如何通过一个输入从两个不同的表中获得结果 - How to get result from two different table with one single input 如何在不同数据库表的一个页面中显示数据 - how to display data in one page from different database table 如何从MySQL数据库中的不同表中获取数据并显示在与php的表中 - How to get data from different tables in MySQL database and display in a table with php 如何从两个不同的php类文件中获取数据到一个文件..? - how to get data from two different php classes files into one file..? (PHP)如何将数据库中2个不同表中的数据显示到引导表中 - (PHP) How to display data from 2 different tables in the database to a bootstrap table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM