Pergunta

É possível ter um MySQLi prepared statement dentro da chamada fetch() de uma declaração anterior? Se não, qual é a melhor maneira de contornar isso?

código Exemplo:

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();
        }
    }
}
Foi útil?

Solução

Você deve ser capaz de fazer isso, embora você faz tem que iniciar uma segunda ligação.

Outras dicas

Esta é a maneira de conexão única:

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();
        }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top