Mysqli_query()调用可以嵌套吗?

时间:2014-11-02 07:21:54

标签: php mysqli

我正在尝试实现以下代码。我想要的是从表prod_id中获取列名为cart_details的产品ID,然后从表prod_id中获取列名为products的产品ID的详细信息。但是这段代码没有返回任何内容。这是否意味着mysqli_query()调用无法嵌套?

<?php
$cart_id=$_POST['q'];
include "connection.php";
$cart_id=mysqli_real_escape_string($link,$cart_id);
$query="select product_id from cart_details where cart_id = $cart_id";
$result=mysqli_query($link,$query) or die(mysqli_error($link));
if($result)
{
    while($row=mysqli_fetch_array($result))
    {
        $prod_id = $row['product_id'];
        $prodDetail = "Select  * from products where prod_id = $prod_id";
        $prodResult = mysqli_query($link,$prodDetails) or die(mysqli_error($link));
        if(!$prodResult){
            echo "There was an error in fetching the product with product ID ".$prod_id;
        }
        else{
            if(mysqli_num_rows($prodResult)==0)
            {
                echo "There is no item in this cart";
            }
            else{
                while($prod=mysqli_fetch_array($prodResult)){
                $prod_name=$prod['prod_name'];
                $prod_price=$prod['prod_price'];
                echo "<tr><td>".$prod_id."</td>";
                echo "<td>".$prod_name."</td>";
                echo "<td>".$prod_price."</td></tr>";
            }

            }
        }
    }

}
else{
    echo "Query Failed";
}
?>

1 个答案:

答案 0 :(得分:0)

检查你的

$query="select product_id from cart_details where cart_id = $cart_id";

您要返回的行数。

您可以通过更改此行if($result)

来实现此目的

到此

if($result && mysqli_num_rows($result)!=0)

注意 mysqli不会自动保护您的申请。使用bindparam