0

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>"; ?> 

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.