PHP 准备语句 - 重置行指针

发布于 2024-11-02 12:10:26 字数 1332 浏览 1 评论 0原文

我有下面的代码,注释应该清楚地解释。我想查询文件中的给定信息,然后返回结果后,将一个字段添加到结果数组中。然后,稍后在我的程序中访问结果数组。

下面代码的问题是,我认为第二次尝试循环时数组位于最后一条记录...并且重置($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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌酱 2024-11-09 12:10:26

我认为这是不可能的。但无论如何,并没有真正令人信服的理由尝试这样做。

保持简单。只需在第一次构建一个数组即可。

$rows = array();
//Loop through all rows, adding a third field
while ( $row = db2_fetch_array ( $stmt1 ) ) {
    $row[2] = "TEST";
    $rows[] = $row;
}
db2_free_result($stmt1);

//Print results
echo "<table border=1>";
foreach($rows as $row) {
    echo "<tr>";
    echo "  <td>" . $row[0] . "</td>";
    echo "  <td>" . $row[1] . "</td>";
    echo "  <td>" . $row[2] . "</td>";
    echo "</tr>";
}
echo "</table>";

最坏的情况是,在构建数组时,这会暂时使内存使用量增加一倍。

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.

$rows = array();
//Loop through all rows, adding a third field
while ( $row = db2_fetch_array ( $stmt1 ) ) {
    $row[2] = "TEST";
    $rows[] = $row;
}
db2_free_result($stmt1);

//Print results
echo "<table border=1>";
foreach($rows as $row) {
    echo "<tr>";
    echo "  <td>" . $row[0] . "</td>";
    echo "  <td>" . $row[1] . "</td>";
    echo "  <td>" . $row[2] . "</td>";
    echo "</tr>";
}
echo "</table>";

This will, at worst, temporarily double your memory usage while the array is being built.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文