11

前のステートメントMySQLi prepared statementの呼び出し内にa を持つことは可能ですか? 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();
        }
    }
}
4

3 に答える 3

16

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

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();
        }
    }
}
于 2012-08-12T14:56:06.270 に答える
2

または使用しますstore_result

于 2009-11-04T17:18:42.130 に答える
1

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

于 2008-09-22T01:47:19.313 に答える