473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

re-using variables

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
19 2361
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

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
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
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
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
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
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
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
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
..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
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

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
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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
1
by: Clarice Almeida Hughes | last post by:
tenho um index onde tenho o link pro arq css, como sao visualizados pelo include todas as paginas aderem ao css linkado no index. so q eu preciso de alguns links com outras cores no css, o q devo...
2
by: JW | last post by:
I wanted have this as part of a flood control script: <? echo ("Flood control in place - please wait " . $floodinterval . " seconds between postings."); sleep(5); // go back two pages echo...
2
by: Frans Schmidt | last post by:
I want to make a new database with several tables, so I did the following: <?php CREATE DATABASE bedrijf; CREATE TABLE werknemers (voornaam varchar(15), achternaam varchar(20), leeftijd...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.