Closing a result set

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ruthsimon
Forum Newbie
Posts: 19
Joined: Mon Aug 19, 2002 5:44 pm

Closing a result set

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What is wrong with mysql_free_result.. seems to be exackly what you are needing.
ruthsimon
Forum Newbie
Posts: 19
Joined: Mon Aug 19, 2002 5:44 pm

kill result set

Post 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) &#123;
   echo 'Could not run query: ' . mysql_error();
   exit;
&#125;
/* 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&#1111;'id'];
echo $row&#1111;'email'];
?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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?
ruthsimon
Forum Newbie
Posts: 19
Joined: Mon Aug 19, 2002 5:44 pm

not sensitive -- just in the way...

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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..
ruthsimon
Forum Newbie
Posts: 19
Joined: Mon Aug 19, 2002 5:44 pm

Post by ruthsimon »

'$row = null' worked. Thank You! Ruth
Post Reply