Re: re-using variables



In article <1172596225.876668.42750@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"Mitesh" <oopsbabies@xxxxxxxxxxx> wrote:

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



Relevant Pages

  • Re: re-using variables
    ... What I have discovered is re-using the $req variable is fine. ... using the $res produces undefined result (sometimes the second query ... or not, not just before a re-use of $res in another query, but also at ... The resource gets freed automatically at the end of the script. ...
    (comp.lang.php)
  • re-using variables
    ... $res = mysql_query; ... What I have discovered is re-using the $req variable is fine. ... In other words since PHP uses garbage collection re-using the same ...
    (comp.lang.php)
  • Re: re-using variables
    ... using the $res produces undefined result (sometimes the second query ... or not, not just before a re-use of $res in another query, but also at ... always a good idea to free something you're going to re-use in case your ... This is sometime after the script ends, ...
    (comp.lang.php)
  • Re: re-using variables
    ... Toby A Inkster wrote: ... like $res1 and $res2 the mysql queries always succeed. ... $res is still going to be set as a result of your first query. ... $res will have the results of the second query. ...
    (comp.lang.php)