By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,350 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,350 IT Pros & Developers. It's quick & easy.

re-using variables

P: n/a
Hi all,

I have the following code:

$req = "SELECT * FROM table1";
$res = mysql_query($req);

if(!$res)
return;

while($line = mysql_fetch_array($res))
{
.............
.............
}

$req = "SELECT * FROM table2";
$res = mysql_query($req);

if(!$res)
return;

while($line = mysql_fetch_array($res))
{
.............
.............
}

What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables
like $res1 and $res2 the mysql queries always succeed. Is the initial
approach of re-using variable wrong if variables contain a
resource(reference type)?

In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?

Feb 27 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
On Tue, 27 Feb 2007 09:10:28 -0800, Mitesh wrote:
What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables like
$res1 and $res2 the mysql queries always succeed. Is the initial
approach of re-using variable wrong if variables contain a
resource(reference type)?

In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?
Google: mysql free result
Feb 27 '07 #2

P: n/a

Ivan Marsh wrote:
On Tue, 27 Feb 2007 09:10:28 -0800, Mitesh wrote:
What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables like
$res1 and $res2 the mysql queries always succeed. Is the initial
approach of re-using variable wrong if variables contain a
resource(reference type)?

In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?

Google: mysql free result
Thanx, but that doesn't address my question. I may have another type
of resource other than mysql result. So, should I be concerned about
re-using reference variables in PHP bound to some resource?

Feb 27 '07 #3

P: n/a
Mitesh wrote:
$res = mysql_query($req);
.............
$res = mysql_query($req);
In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?
Using the same variable twice is fine. Don't bother too much about the
garbage collector. Since it removes variables when you don't use them
anymore, it never gets in the way.

You probably have another problem which causes things to fail sometimes.

Sjoerd
Feb 27 '07 #4

P: n/a
On Tue, 27 Feb 2007 09:57:27 -0800, Mitesh wrote:
>
Ivan Marsh wrote:
>On Tue, 27 Feb 2007 09:10:28 -0800, Mitesh wrote:
What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables like
$res1 and $res2 the mysql queries always succeed. Is the initial
approach of re-using variable wrong if variables contain a
resource(reference type)?

In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?

Google: mysql free result

Thanx, but that doesn't address my question. I may have another type
of resource other than mysql result. So, should I be concerned about
re-using reference variables in PHP bound to some resource?
I haven't had any trouble with it... but then I tend to destroy/create
anything I'm going to re-use.

Feb 27 '07 #5

P: n/a
In article <11*********************@k78g2000cwa.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
Hi all,

I have the following code:

$req = "SELECT * FROM table1";
$res = mysql_query($req);

if(!$res)
return;

while($line = mysql_fetch_array($res))
{
.............
.............
}

$req = "SELECT * FROM table2";
$res = mysql_query($req);

if(!$res)
return;

while($line = mysql_fetch_array($res))
{
.............
.............
}

What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables
like $res1 and $res2 the mysql queries always succeed. Is the initial
approach of re-using variable wrong if variables contain a
resource(reference type)?

In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?
Pass. But I systematically do mysql_free_result($res) whether I need to
or not, not just before a re-use of $res in another query, but also at
the end of the script. I don't know how it works internally or how much
one needs to worry about this.
Feb 27 '07 #6

P: n/a
On Tue, 27 Feb 2007 20:44:47 +0000, Tim Streater wrote:
In article <11*********************@k78g2000cwa.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
>>
What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables
like $res1 and $res2 the mysql queries always succeed. Is the initial
approach of re-using variable wrong if variables contain a
resource(reference type)?

In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?

Pass. But I systematically do mysql_free_result($res) whether I need to
or not, not just before a re-use of $res in another query, but also at
the end of the script. I don't know how it works internally or how much
one needs to worry about this.
The resource gets freed automatically at the end of the script. It's
always a good idea to free something you're going to re-use in case your
code isn't working properly you could be left with old data still in the
resource that's hiding where your real issue is.
Feb 27 '07 #7

P: n/a
Mitesh wrote:
What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables
like $res1 and $res2 the mysql queries always succeed.
Well, obviously.

You have:

$res = mysql_query("SELECT * FROM table1");
...
$res = mysql_query("SELECT * FROM table2");
if(!$res)
return;

$res is still going to be set as a result of your first query.

Use unset($res) between each query.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 27 '07 #8

P: n/a
Ivan Marsh wrote:
On Tue, 27 Feb 2007 20:44:47 +0000, Tim Streater wrote:
>In article <11*********************@k78g2000cwa.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
>>What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables
like $res1 and $res2 the mysql queries always succeed. Is the initial
approach of re-using variable wrong if variables contain a
resource(reference type)?

In other words since PHP uses garbage collection re-using the same
variable can cause problems. Is this true?
Pass. But I systematically do mysql_free_result($res) whether I need to
or not, not just before a re-use of $res in another query, but also at
the end of the script. I don't know how it works internally or how much
one needs to worry about this.

The resource gets freed automatically at the end of the script. It's
always a good idea to free something you're going to re-use in case your
code isn't working properly you could be left with old data still in the
resource that's hiding where your real issue is.

One correction. It gets freed when the garbage collector runs. This is
sometime after the script ends, but may not be immediately.

I always free my results, close connections, and generally clean up for
myself. It's just good programming practice.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 27 '07 #9

P: n/a
Toby A Inkster wrote:
Mitesh wrote:
>What I have discovered is re-using the $req variable is fine. While re-
using the $res produces undefined result (sometimes the second query
works fine and sometimes it doesn't. When I use different variables
like $res1 and $res2 the mysql queries always succeed.

Well, obviously.

You have:

$res = mysql_query("SELECT * FROM table1");
...
$res = mysql_query("SELECT * FROM table2");
if(!$res)
return;

$res is still going to be set as a result of your first query.

Use unset($res) between each query.
No, Toby.

$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.

But it's still not good programming practice.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 27 '07 #10

P: n/a
..oO(Jerry Stuckle)
>Toby A Inkster wrote:
>You have:

$res = mysql_query("SELECT * FROM table1");
...
$res = mysql_query("SELECT * FROM table2");
if(!$res)
return;

$res is still going to be set as a result of your first query.

Use unset($res) between each query.

No, Toby.

$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.
Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.

| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.

http://bugs.php.net/bug.php?id=35793

Micha
Feb 28 '07 #11

P: n/a
Michael Fesser wrote:
.oO(Jerry Stuckle)
>Toby A Inkster wrote:
>>You have:

$res = mysql_query("SELECT * FROM table1");
...
$res = mysql_query("SELECT * FROM table2");
if(!$res)
return;

$res is still going to be set as a result of your first query.

Use unset($res) between each query.
No, Toby.

$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.

Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.

| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.

http://bugs.php.net/bug.php?id=35793

Micha
We're not talking PDO , Micha.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 28 '07 #12

P: n/a

Jerry Stuckle wrote:
Michael Fesser wrote:
.oO(Jerry Stuckle)
Toby A Inkster wrote:

You have:

$res = mysql_query("SELECT * FROM table1");
...
$res = mysql_query("SELECT * FROM table2");
if(!$res)
return;

$res is still going to be set as a result of your first query.

Use unset($res) between each query.
No, Toby.

$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.
Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.

| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.

http://bugs.php.net/bug.php?id=35793

Micha

We're not talking PDO , Micha.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)

Feb 28 '07 #13

P: n/a
In article <11**********************@h3g2000cwc.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
Jerry Stuckle wrote:
Michael Fesser wrote:
.oO(Jerry Stuckle)
>
>Toby A Inkster wrote:
>>
>>You have:
>>>
>> $res = mysql_query("SELECT * FROM table1");
>> ...
>> $res = mysql_query("SELECT * FROM table2");
>> if(!$res)
>> return;
>>>
>>$res is still going to be set as a result of your first query.
>>>
>>Use unset($res) between each query.
>No, Toby.
>>
>$res will have the results of the second query. The resource returned
>by the second query (or false) will overwrite what was in $res.
>
Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.
>
| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.
>
http://bugs.php.net/bug.php?id=35793
>
Micha
We're not talking PDO , Micha.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)
My understanding was that, if you re-use $res without a call to
mysql_free_result, it re-uses what $res is, which is a pointer to a
result set, but doing that does *not* free up the space used by the
result set. At least that's how it used to be. I don't know if they have
improved the internals such that mysql_free_result is now a dummy call,
or whether that is inherently impossible because mysql itself holds the
result set.

Perhaps a guru can enlighten us.

-- tim
Feb 28 '07 #14

P: n/a

Tim Streater wrote:
In article <11**********************@h3g2000cwc.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
Jerry Stuckle wrote:
Michael Fesser wrote:
.oO(Jerry Stuckle)

Toby A Inkster wrote:
>
>You have:
>>
> $res = mysql_query("SELECT * FROM table1");
> ...
> $res = mysql_query("SELECT * FROM table2");
> if(!$res)
> return;
>>
>$res is still going to be set as a result of your first query.
>>
>Use unset($res) between each query.
No, Toby.
>
$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.

Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.

| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.

http://bugs.php.net/bug.php?id=35793

Micha
>
We're not talking PDO , Micha.
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)

My understanding was that, if you re-use $res without a call to
mysql_free_result, it re-uses what $res is, which is a pointer to a
result set, but doing that does *not* free up the space used by the
result set. At least that's how it used to be. I don't know if they have
improved the internals such that mysql_free_result is now a dummy call,
or whether that is inherently impossible because mysql itself holds the
result set.

Perhaps a guru can enlighten us.

-- tim
My concern is not if the previous resource is deallocated or not. I
don't have a huge result set returned. It will eventually be freed.

What I am really afraid is the working of my current resource pointed
to by $res which I have experienced to be not working occasionally as
I stated that my second fetch array function suceeds some times and
fails other times.

Can the following happen?

When I overwrite $res variable (whatever the variable maybe a pointer
or a reference). Suppose the garbage collector kicks in and finds that
the previous resource isn't owned by any variable and tries to free
it. When it tries to free, it accesses the $res variable. But now the
$res variable owns another resource i.e. my second mysql result set
and wrongly frees the second resource hence my second
mysql_fetch_array fails.

I am sure that I have read that in PHP variables that contain
resources are reference variables (similar to pointers) unlike other
variables like the $req variable storing a string. We can indded make
a variable act like a reference using an ampersand i think.

Yes it seems we need a guru here.

So my would be question to our guru is can we safely re-use variables
that are references?

Right now I resolved the situation using two different variable names
before I got the suggestion on using mysql_free_result. The product is
already shipped and it seems to be working.

Feb 28 '07 #15

P: n/a

Mitesh wrote:
Tim Streater wrote:
In article <11**********************@h3g2000cwc.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
Jerry Stuckle wrote:
Michael Fesser wrote:
.oO(Jerry Stuckle)
>
>Toby A Inkster wrote:
>>
>>You have:
>>>
>> $res = mysql_query("SELECT * FROM table1");
>> ...
>> $res = mysql_query("SELECT * FROM table2");
>> if(!$res)
>> return;
>>>
>>$res is still going to be set as a result of your first query.
>>>
>>Use unset($res) between each query.
>No, Toby.
>>
>$res will have the results of the second query. The resource returned
>by the second query (or false) will overwrite what was in $res.
>
Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.
>
| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.
>
http://bugs.php.net/bug.php?id=35793
>
Micha

We're not talking PDO , Micha.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
>
Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)
My understanding was that, if you re-use $res without a call to
mysql_free_result, it re-uses what $res is, which is a pointer to a
result set, but doing that does *not* free up the space used by the
result set. At least that's how it used to be. I don't know if they have
improved the internals such that mysql_free_result is now a dummy call,
or whether that is inherently impossible because mysql itself holds the
result set.

Perhaps a guru can enlighten us.

-- tim

My concern is not if the previous resource is deallocated or not. I
don't have a huge result set returned. It will eventually be freed.

What I am really afraid is the working of my current resource pointed
to by $res which I have experienced to be not working occasionally as
I stated that my second fetch array function suceeds some times and
fails other times.

Can the following happen?

When I overwrite $res variable (whatever the variable maybe a pointer
or a reference). Suppose the garbage collector kicks in and finds that
the previous resource isn't owned by any variable and tries to free
it. When it tries to free, it accesses the $res variable. But now the
$res variable owns another resource i.e. my second mysql result set
and wrongly frees the second resource hence my second
mysql_fetch_array fails.

I am sure that I have read that in PHP variables that contain
resources are reference variables (similar to pointers) unlike other
variables like the $req variable storing a string. We can indded make
a variable act like a reference using an ampersand i think.

Yes it seems we need a guru here.

So my would be question to our guru is can we safely re-use variables
that are references?

Right now I resolved the situation using two different variable names
before I got the suggestion on using mysql_free_result. The product is
already shipped and it seems to be working.
Sorry some corrections on my part:

I had in my post said that my second query sometimes succeeds and
sometimes doesn't. The query succeeds in both occasions however
mysql_fetch_array sometimes succeeds and sometimes fails.

Feb 28 '07 #16

P: n/a
Mitesh wrote:
Jerry Stuckle wrote:
>Michael Fesser wrote:
>>.oO(Jerry Stuckle)

Toby A Inkster wrote:

You have:
>
$res = mysql_query("SELECT * FROM table1");
...
$res = mysql_query("SELECT * FROM table2");
if(!$res)
return;
>
$res is still going to be set as a result of your first query.
>
Use unset($res) between each query.
No, Toby.

$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.
Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.

| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.

http://bugs.php.net/bug.php?id=35793

Micha
We're not talking PDO , Micha.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)
THE VARIABLE IS NOT THE RESOURCE! The variable can be reused. But
there are MySQL resources allocated. mysql_free_result() frees up these
resources. If you don't call it, the resources won't be freed up until
the garbage collector runs. This will be sometime after the last
reference to the resource is destroyed/reused or whatever.

Now this is not likely to cause a problem in most systems. However, in
some circumstances (i.e. the resource caused rows or tables to be locked
in MySQL), other requests could be delayed until the garbage collector
runs. And in extremely busy systems, this could occasionally cause
other requests to time out because the locks are held too long.

But these are problems you wouldn't generally notice in most websites.
Nonetheless, it's always better to clean up after yourself and free the
resources as soon as you're done with them. Not only is it good
programming practice, it decreases the load on the server (because
resources are not being held longer than necessary).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 28 '07 #17

P: n/a
Tim Streater wrote:
In article <11**********************@h3g2000cwc.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
>Jerry Stuckle wrote:
>>Michael Fesser wrote:
.oO(Jerry Stuckle)

Toby A Inkster wrote:
>
>You have:
>>
> $res = mysql_query("SELECT * FROM table1");
> ...
> $res = mysql_query("SELECT * FROM table2");
> if(!$res)
> return;
>>
>$res is still going to be set as a result of your first query.
>>
>Use unset($res) between each query.
No, Toby.
>
$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.
Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.

| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.

http://bugs.php.net/bug.php?id=35793

Micha
We're not talking PDO , Micha.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)

My understanding was that, if you re-use $res without a call to
mysql_free_result, it re-uses what $res is, which is a pointer to a
result set, but doing that does *not* free up the space used by the
result set. At least that's how it used to be. I don't know if they have
improved the internals such that mysql_free_result is now a dummy call,
or whether that is inherently impossible because mysql itself holds the
result set.

Perhaps a guru can enlighten us.

-- tim
Tim,

I'm not a guru. But when you make certain calls to MySQL, it will hold
certain resources for the result set. mysql_free_result() is anything
BUT a dummy call.

Similar calls exist in most SQL databases for the same reason.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 28 '07 #18

P: n/a
Mitesh wrote:
Mitesh wrote:
>Tim Streater wrote:
>>In article <11**********************@h3g2000cwc.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:

Jerry Stuckle wrote:
Michael Fesser wrote:
>.oO(Jerry Stuckle)
>>
>>Toby A Inkster wrote:
>>>
>>>You have:
>>>>
>>> $res = mysql_query("SELECT * FROM table1");
>>> ...
>>> $res = mysql_query("SELECT * FROM table2");
>>> if(!$res)
>>> return;
>>>>
>>>$res is still going to be set as a result of your first query.
>>>>
>>>Use unset($res) between each query.
>>No, Toby.
>>>
>>$res will have the results of the second query. The resource returned
>>by the second query (or false) will overwrite what was in $res.
>Try that with PDO and it will crash most likely. Overwriting $res will
>not necessarily free the previous result set, which might then lead to
>MySQL complaining about an unbuffered query or something like that.
>>
>| You cannot use the same variable for a PDOStatement object twice. As
>| others have pointed out it works when you set this variable to null in
>| between.
>>
>http://bugs.php.net/bug.php?id=35793
>>
>Micha
We're not talking PDO , Micha.
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)
My understanding was that, if you re-use $res without a call to
mysql_free_result, it re-uses what $res is, which is a pointer to a
result set, but doing that does *not* free up the space used by the
result set. At least that's how it used to be. I don't know if they have
improved the internals such that mysql_free_result is now a dummy call,
or whether that is inherently impossible because mysql itself holds the
result set.

Perhaps a guru can enlighten us.

-- tim
My concern is not if the previous resource is deallocated or not. I
don't have a huge result set returned. It will eventually be freed.

What I am really afraid is the working of my current resource pointed
to by $res which I have experienced to be not working occasionally as
I stated that my second fetch array function suceeds some times and
fails other times.

Can the following happen?

When I overwrite $res variable (whatever the variable maybe a pointer
or a reference). Suppose the garbage collector kicks in and finds that
the previous resource isn't owned by any variable and tries to free
it. When it tries to free, it accesses the $res variable. But now the
$res variable owns another resource i.e. my second mysql result set
and wrongly frees the second resource hence my second
mysql_fetch_array fails.

I am sure that I have read that in PHP variables that contain
resources are reference variables (similar to pointers) unlike other
variables like the $req variable storing a string. We can indded make
a variable act like a reference using an ampersand i think.

Yes it seems we need a guru here.

So my would be question to our guru is can we safely re-use variables
that are references?

Right now I resolved the situation using two different variable names
before I got the suggestion on using mysql_free_result. The product is
already shipped and it seems to be working.

Sorry some corrections on my part:

I had in my post said that my second query sometimes succeeds and
sometimes doesn't. The query succeeds in both occasions however
mysql_fetch_array sometimes succeeds and sometimes fails.
No, the scenario you describe should not happen. If it does, it's a bug
in MySQL. If the resource isn't referenced any more, how would the
garbage collector know to go back to $res, for instance? And if it did,
it would be a bug in PHP.

You never gave us any error messages, etc., so we have no idea what
might have happened.

And BTW - I've done the same many times before, mostly because I forgot
to call mysql_free_result() when writing the code. I've never suffered
any problems because of it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 28 '07 #19

P: n/a
In article <-Z******************************@comcast.com>,
Jerry Stuckle <js*******@attglobal.netwrote:
Tim Streater wrote:
In article <11**********************@h3g2000cwc.googlegroups. com>,
"Mitesh" <oo********@hotmail.comwrote:
Jerry Stuckle wrote:
Michael Fesser wrote:
.oO(Jerry Stuckle)

Toby A Inkster wrote:

You have:
>
$res = mysql_query("SELECT * FROM table1");
...
$res = mysql_query("SELECT * FROM table2");
if(!$res)
return;
>
$res is still going to be set as a result of your first query.
>
Use unset($res) between each query.
No, Toby.

$res will have the results of the second query. The resource returned
by the second query (or false) will overwrite what was in $res.
Try that with PDO and it will crash most likely. Overwriting $res will
not necessarily free the previous result set, which might then lead to
MySQL complaining about an unbuffered query or something like that.

| You cannot use the same variable for a PDOStatement object twice. As
| others have pointed out it works when you set this variable to null in
| between.

http://bugs.php.net/bug.php?id=35793

Micha
We're not talking PDO , Micha.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Ok if we need to call, mysql_free_result for mysql resources doesn't
that mean every other type of resources bound this way to a variable
must have a freeing function that has to be called before the variable
can be re-used? So doesn't it coincide with what I am saying that
variables bound to resources when re-used may cause problems. (and
according to the others post the variables can be re-used if the
previously allocated resource is freed)
My understanding was that, if you re-use $res without a call to
mysql_free_result, it re-uses what $res is, which is a pointer to a
result set, but doing that does *not* free up the space used by the
result set. At least that's how it used to be. I don't know if they have
improved the internals such that mysql_free_result is now a dummy call,
or whether that is inherently impossible because mysql itself holds the
result set.

Perhaps a guru can enlighten us.

-- tim

Tim,

I'm not a guru. But when you make certain calls to MySQL, it will hold
certain resources for the result set. mysql_free_result() is anything
BUT a dummy call.

Similar calls exist in most SQL databases for the same reason.
Jerry,

Thanks. I'll stick to my good programming practice then, which is to
always free it even as the script exits.
Feb 28 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.