可以使用多个/嵌套的MySQLi语句吗?
时间:2020-03-06 14:31:12 来源:igfitidea点击:
在上一条语句的fetch()调用中是否可能有MySQLi预处理语句?如果没有,最好的解决方法是什么?
示例代码:
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();
}
}
}
解决方案
尽管我们必须启动第二个连接,但我们应该能够做到这一点。
或者使用store_result。

