複数の/ネストされた 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();
        }
    }
}
役に立ちましたか?

解決

2 番目の接続を開始する必要がありますが、それはできるはずです。

他のヒント

これは単一の接続方法です。

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