Re: re-using variables




Mitesh wrote:
Tim Streater wrote:
In article <1172668483.913253.134510@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"Mitesh" <oopsbabies@xxxxxxxxxxx> 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.
jstucklex@xxxxxxxxxxxxx
==================

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.

.



Relevant Pages

  • Re: re-using variables
    ... $res is still going to be set as a result of your first query. ... Use unsetbetween each query. ... previously allocated resource is freed) ... resources are reference variables unlike other ...
    (comp.lang.php)
  • Re: re-using variables
    ... $res is still going to be set as a result of your first query. ... Use unsetbetween each query. ... variables bound to resources when re-used may cause problems. ... if you re-use $res without a call to ...
    (comp.lang.php)
  • Re: re-using variables
    ... $res is still going to be set as a result of your first query. ... Use unsetbetween each query. ... Ok if we need to call, mysql_free_result for mysql resources doesn't ... if you re-use $res without a call to ...
    (comp.lang.php)
  • Re: re-using variables
    ... $res is still going to be set as a result of your first query. ... Use unsetbetween each query. ... previously allocated resource is freed) ... So my would be question to our guru is can we safely re-use variables ...
    (comp.lang.php)
  • [PATCH] vsprintf: use %pR, %pr instead of %pRt, %pRf
    ... window->resource.start - offset, ... if (insert_resource(root, res)) { ... Invalidate the resource to prevent ... - 's' For symbolic direct pointers without offset ...
    (Linux-Kernel)