MySQLi准备声明在另一个准备声明中是否可能?

时间:2013-07-09 12:31:57

标签: php mysqli prepared-statement

我想知道是否可以在另一个准备语句中使用prepare语句。

例如:

$stmt = $connection->stmt_init();
if ($stmt->prepare("SELECT Test1 FROM TestTable1"))
{

$stmt->execute();
$stmt->bind_result($test1);

while($stmt->fetch())
{
   echo $test1;

   $stmt2 = $connection->stmt_init();
   if ($stmt2->prepare("SELECT Test2 FROM TestTable2"))
   {

      $stmt2->execute();
      $stmt2->bind_result($test2);

      while($stmt2->fetch())
      {
         echo $test2;
      }
   }
}

这可能吗?

修改

这是我的代码。

$stmt = $connection->stmt_init();

if ($stmt->prepare("
SELECT DISTINCT a.`ProductId`, a.`ProductCode`, a.`ProductRetailPrice`, a.`ProductPrice`, a.`ProductOffer`, a.`ProductTopSeler`, a.`ProductStrass`, a.`ProductStatus`, b.`SubProductThumb`
FROM `Products` as a, `SubProducts` as b
WHERE b.ProductId=a.ProductId
GROUP BY a.`ProductId`
"))
{

$stmt->execute();
$stmt->bind_result($product_id, $product_code, $product_retail_price, $product_price, $product_offer, $product_top_seler, $product_strass, $product_status, $sub_product_thumb);

while($stmt->fetch())
{
    if ($current_id!=$product_id)
    {
        $current_id = $product_id;

        echo $product_code.'<br />'.$product_retail_price.'<br />';

        $sub_stmt = $connection->stmt_init();

        if ($sub_stmt->prepare("
            SELECT SubProductId, SubProductPieces, SubProductStatus, SubProductRingSize6, SubProductRingSize7, SubProductRingSize8, SubProductRingSize9, c1.ColorHex as Color1, c1.ColorName as ColorName1, c2.ColorHex as Color2, c2.ColorName as ColorName2
            FROM SubProducts
            INNER JOIN Colors c1 ON c1.ColorId=SubProducts.SubProductColor1
            INNER JOIN Colors c2 ON c2.ColorId=SubProducts.SubProductColor2
            WHERE ProductId=?
        "))
        {
            $sub_stmt->bind_param('s', $product_id);
            $sub_stmt->execute();
            $sub_stmt->bind_result($sub_product_id, $sub_product_pieces, $sub_product_status, $sub_product_ring_size_6, $sub_product_ring_size_7, $sub_product_ring_size_8, $sub_product_ring_size_9, $sub_product_color_hex_1, $sub_product_color_name_1, $sub_product_color_hex_2, $sub_product_color_name_2);

            while($sub_stmt->fetch())
            {
                echo $sub_product_pieces;
            }
        }
    }
}

$stmt->close();
}

如果在另一个准备声明中没有任何方式准备声明,可以查询吗?或者还有另一种方法吗?

0 个答案:

没有答案