views:

523

answers:

2

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?

Example code:

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();
      }
  }
}
+1  A: 

You should be able to do that, although you make have to start a second connection.

Kibbee
A second connection does work, is this the best way?
Gilean
A: 

Or use store_result http://php.net/manual/en/mysqli.store-result.php

peterbriers