php准备stmt问题-在每个结果循环更新数据
如果我想通过准备好的 stmt 每个查询循环更新数据怎么办,但为什么会失败。错误消息是“在新语句准备发生之前必须获取所有数据”
$link = mysqli_connect("localhost", "admin", "", "test");
if (!$link) {
die('Connect Error: ' . mysqli_connect_error());
}
//field_1 is PK
if ($stmt = mysqli_prepare($link, "SELECT field_1, field_2 FROM table_data")) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $col1, $col2);
while (mysqli_stmt_fetch($stmt)) {
$updateC= "update table_data set field_3=? where field_1=?"
if ($stmt2= mysqli_prepare($link, $updateC)) {
mysqli_stmt_execute($stmt2);
$status='test'; //get return value from function
mysqli_stmt_bind_param($stmt2, 'ss', $status, $col1);
}
mysqli_stmt_close($stmt2);
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
what if I want to update data via prepared stmt each query loop, but why fails. the error msg is "All data must be fetched before a new statement prepare takes place "
$link = mysqli_connect("localhost", "admin", "", "test");
if (!$link) {
die('Connect Error: ' . mysqli_connect_error());
}
//field_1 is PK
if ($stmt = mysqli_prepare($link, "SELECT field_1, field_2 FROM table_data")) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $col1, $col2);
while (mysqli_stmt_fetch($stmt)) {
$updateC= "update table_data set field_3=? where field_1=?"
if ($stmt2= mysqli_prepare($link, $updateC)) {
mysqli_stmt_execute($stmt2);
$status='test'; //get return value from function
mysqli_stmt_bind_param($stmt2, 'ss', $status, $col1);
}
mysqli_stmt_close($stmt2);
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在循环中准备第二条语句,该循环从第一条语句中检索数据。您也许可以通过创建两个连接来解决此问题。请参阅这篇文章:
http://osdir.com/ ml/php.zend.framework.db/2007-07/msg00027.html
您也许还可以将第一个查询中的所有行读取到一个数组中,然后循环该数组,放置第二个查询在那个循环中。
提前准备第二个语句也许是可能的(我还没有尝试过)。在循环中时,将变量绑定到它并执行它。像这样的东西(完全未经测试!):
You are preparing a second statement within a loop which is retrieving data from the first statement. You might be able to solve this by creating two connections. See this post:
http://osdir.com/ml/php.zend.framework.db/2007-07/msg00027.html
You might also be able to read all the rows from the first query into an array, and then loop over the array, placing your second query in that loop.
It may be possible (and I haven't tried this), to prepare the second statement in advance. When in the loop, bind the variables to it and execute it. Something like this (completely untested!):