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>";
?>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.