Possible to use multiple/nested MySQLi statements?

前端 未结 3 1828
闹比i
闹比i 2021-01-11 16:03

Is it possible to have a MySQLi prepared statement within the fetch() call of a previous statement? If not, what\'s the best way around it?

3条回答
  •  别那么骄傲
    2021-01-11 16:26

    This is the single connection way:

    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();
            }
        }
    }
    

提交回复
热议问题