PHP 准备语句 - 重置行指针
我有下面的代码,注释应该清楚地解释。我想查询文件中的给定信息,然后返回结果后,将一个字段添加到结果数组中。然后,稍后在我的程序中访问结果数组。
下面代码的问题是,我认为第二次尝试循环时数组位于最后一条记录...并且重置($stmt1)失败,因为$stmt1“不是数组对象”...
<?php
$sql1 = "SELECT dpdpt, SUM(dpbir) FROM dwhlib.dwdptstr WHERE dpdpt < '312' GROUP BY dpdpt ";
//Setup connection
$conn_resource = db2_connect ( "*LOCAL", "", "" );
//Verify connection successful
if (! $conn_resource) {
echo "Connection failed. SQL Err:";
echo db2_conn_error ();
echo "<br/>";
echo db2_conn_errormsg ();
exit ();
}
//Prepare the stmt
$stmt1 = db2_prepare ( $conn_resource, $sql1 );
//Execute the prepared statement
if (! db2_execute ( $stmt1 )) {
echo 'The db2 execute failed. ';
echo 'SQLSTATE value: ' . db2_stmt_error ();
echo ' Message: ' . db2_stmt_errormsg ();
exit ();
}
//Loop through all rows, adding a third field
while ( $row1 = &db2_fetch_array ( $stmt1 ) ) {
$row1[2] = "TEST";
}
//Reset stmt or array back to first record
//??
//Print results
echo "<table border=1>";
while ( $row = db2_fetch_array ( $stmt1 ) ) {
echo "<tr>";
echo " <td>" . $row[0] . "</td>";
echo " <td>" . $row[1] . "</td>";
echo " <td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
I have the below code, which the comments should clearly explain. I want to query a file for given information, and then once the results are returned, add a field onto the resulting array. Then, access the resulting array later in my program.
The problem with the code below is, i think the array is at the last record the second time i attempt to loop... and reset($stmt1) fails because $stmt1 is "not an array object"...
<?php
$sql1 = "SELECT dpdpt, SUM(dpbir) FROM dwhlib.dwdptstr WHERE dpdpt < '312' GROUP BY dpdpt ";
//Setup connection
$conn_resource = db2_connect ( "*LOCAL", "", "" );
//Verify connection successful
if (! $conn_resource) {
echo "Connection failed. SQL Err:";
echo db2_conn_error ();
echo "<br/>";
echo db2_conn_errormsg ();
exit ();
}
//Prepare the stmt
$stmt1 = db2_prepare ( $conn_resource, $sql1 );
//Execute the prepared statement
if (! db2_execute ( $stmt1 )) {
echo 'The db2 execute failed. ';
echo 'SQLSTATE value: ' . db2_stmt_error ();
echo ' Message: ' . db2_stmt_errormsg ();
exit ();
}
//Loop through all rows, adding a third field
while ( $row1 = &db2_fetch_array ( $stmt1 ) ) {
$row1[2] = "TEST";
}
//Reset stmt or array back to first record
//??
//Print results
echo "<table border=1>";
while ( $row = db2_fetch_array ( $stmt1 ) ) {
echo "<tr>";
echo " <td>" . $row[0] . "</td>";
echo " <td>" . $row[1] . "</td>";
echo " <td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不可能的。但无论如何,并没有真正令人信服的理由尝试这样做。
保持简单。只需在第一次构建一个数组即可。
最坏的情况是,在构建数组时,这会暂时使内存使用量增加一倍。
I don't think that's possible. But there's not really a compelling reason to try to do it that way anyway.
Keep it simple. Just build an array the first time through.
This will, at worst, temporarily double your memory usage while the array is being built.