2008-09-22 39 views
11

Có thể có một số MySQLi prepared statement trong cuộc gọi fetch() của một tuyên bố trước đó không? Nếu không, cách tốt nhất xung quanh nó là gì?Có thể sử dụng nhiều câu lệnh MySQL/lồng nhau không?

Ví dụ mã:

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

Trả lời

1

Bạn sẽ có thể làm điều đó, mặc dù bạn thực hiện phải bắt đầu một kết nối thứ hai.

+1

Một kết nối thứ hai làm việc, đây là cách tốt nhất? – Gilean

13

Đây là cách kết nối duy nhất:

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(); 
     } 
    } 
} 
Các vấn đề liên quan