¿Es posible utilizar declaraciones MySQLi múltiples/anidadas?
Pregunta
¿Es posible tener un MySQLi prepared statement
dentro de fetch()
llamada de una declaración previa?Si no, ¿cuál es la mejor manera de evitarlo?
Código de ejemplo:
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();
}
}
}
Solución
Deberías poder hacerlo, aunque tendrás que iniciar una segunda conexión.
Otros consejos
Esta es la forma de conexión ú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();
}
}
}
O usar store_result
.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow