من الممكن استخدام متعددة/المتداخلة MySQLi التصريحات ؟

StackOverflow https://stackoverflow.com/questions/112775

  •  02-07-2019
  •  | 
  •  

سؤال

هل من الممكن أن يكون MySQLi prepared statement داخل fetch() دعوة من العبارة السابقة ؟ إذا لم يكن كذلك ، ما هي أفضل طريقة للتغلب على ذلك ؟

رمز المثال:

if($stmt = $link->prepare("SELECT item FROM data WHERE id = ?")) {
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->bind_result($item);
    while( $stmt->fetch() ) {
        /* Other code here */
        $itemSummary = $item + $magic;
        if($stmt2 = $link->prepare("INSERT INTO summaries (itemID, summary) VALUES (?, ?)")) {
            $stmt2->bind_param("is", $itemID, $itemSummary);
            $stmt2->execute();
            $stmt2->close();
        }
    }
}
هل كانت مفيدة؟

المحلول

يجب أن تكون قادرة على القيام بذلك ، على الرغم من أن لديهم لبدء اتصال ثاني.

نصائح أخرى

هذا هو واحد طريقة الاتصال:

if($stmt = $link->prepare("SELECT item FROM data WHERE id = ?")) {
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->store_result(); // <-- this
    $stmt->bind_result($item);
    while( $stmt->fetch() ) {
        /* Other code here */
        $itemSummary = $item + $magic;
        if($stmt2 = $link->prepare("INSERT INTO summaries (itemID, summary) VALUES (?, ?)")) {
            $stmt2->bind_param("is", $itemID, $itemSummary);
            $stmt2->execute();
            $stmt2->store_result(); // <-- this
            /*DO WHATEVER WITH STMT2*/
            $stmt2->close();
        }
    }
}

أو استخدام store_result.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top