【问题标题】:how to get multiple data from two different table from one database in php如何在php中从一个数据库的两个不同表中获取多个数据
【发布时间】:2018-12-22 01:19:59
【问题描述】:

我正在创建购物网站,现在我有一个表调用产品,其中存储了所有产品数据,并且我有不同的表购物车,其中存储了所有添加到购物车的产品,所以基本上在购物车中我存储了 productid, userid 所以现在在名为 checkout 的页面上,我想检索数据,我们将从购物车表中获取特定登录用户的 productid,并且从表 products 我们将显示添加到购物车的所有产品,所以基本上从购物车表中我们将获取添加到购物车和餐桌产品的产品,我们将展示它们。现在的问题是我已经检索到数据,但它只显示一个项目而不是多个项目。提前致谢

这是我的代码:

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

【问题讨论】:

    标签: php mysql database html


    【解决方案1】:

    您应该使用另一个且只有一个查询与 JOIN 语句。

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

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

    【讨论】:

      【解决方案2】:

      您可以使用内部连接来使用单个查询

        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
      

      【讨论】:

        【解决方案3】:

        我将添加这项技术,它更接近我使用的技术,并且没有其他人完全一样。

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

        【讨论】:

          猜你喜欢
          • 2018-07-04
          • 1970-01-01
          • 1970-01-01
          • 2019-02-09
          • 1970-01-01
          • 2014-06-10
          • 1970-01-01
          • 1970-01-01
          • 2019-08-06
          相关资源
          最近更新 更多