2008-09-22 14 views
11

¿Es posible tener un MySQLi prepared statement dentro de la llamada fetch() de una declaración anterior? Si no, ¿cuál es la mejor manera de evitarlo?Posible usar declaraciones MySQLi múltiples/anidadas?

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

Respuesta

1

Usted debe ser capaz de hacer eso, aunque se haga tiene que comenzar una segunda conexión.

+1

Una segunda conexión funciona, es ésta la mejor manera? – Gilean

13

Ésta es la única forma de conexión:

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