Page 1 of 1
Closing a result set
Posted: Fri Dec 03, 2004 12:19 pm
by ruthsimon
This should be easy:
I have a query result set and I've already used the results of the set. How do I then get rid of the result set from that query later on in the same script?
It's obviously not mysql_free_result. Any function I'm missing?
Thanks, Ruth
Posted: Fri Dec 03, 2004 4:25 pm
by John Cartwright
What is wrong with mysql_free_result.. seems to be exackly what you are needing.
kill result set
Posted: Sun Dec 05, 2004 12:26 am
by ruthsimon
I want to kill the result set, so I can't even echo any data from it. mysql_free_result just releases the memory, but the result set still exists.
From the PHP manual:
Example 1. A mysql_free_result() example
Code: Select all
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
/* Use the result, assuming we're done with it afterwords */
$row = mysql_fetch_assoc($result);
/* Now we free up the result and continue on with our script */
mysql_free_result($result);
echo $rowї'id'];
echo $rowї'email'];
?>
Posted: Sun Dec 05, 2004 8:11 am
by kettle_drum
Well in most cases you just release the memory, if you want to get rid of it completly you will have to write over it - are the results that sensitive that they cant be left in memory?
not sensitive -- just in the way...
Posted: Sun Dec 05, 2004 8:27 am
by ruthsimon
This is for a calendar of upcoming events. The common header for the entire site calls a SQL statement and then displays the next upcoming event (this is the original result set).
One script on the site is the form to input new events. The first time a user sees the form it should not be populated -- then if there's a validation issue, the form reappears with the user's POST information (so there's a 'value = $variable' statement in each form field).
The problem is that the first time the user sees the form, it's populated with the record from the result set in the header.
I attempted to overwrite the results by setting up a new SQL statement that would pull 0 records using the same variable names that are in the header request, but it didn't work.
Any suggestions?
Thanks, Ruth
Posted: Sun Dec 05, 2004 9:17 am
by kettle_drum
Give the variables different names? When passing the variables back to the form if there was an error call the fields 'err_name', 'err_event' etc. And you should really also pass back a variable called 'error' or something similar and then you can check to see if there was an error and know if you should populate the field.
Posted: Sun Dec 05, 2004 9:22 am
by timvw
actually, if i understand the code a little, you don't want only to release memory of the resultset ... but also the data that was fetched...
so, if you retrieve data into $row with myqsl_fetch_* , you would need to do $row = null to make sure that data is changed too..
Posted: Sun Dec 05, 2004 10:00 am
by ruthsimon
'$row = null' worked. Thank You! Ruth