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?

.



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: re-using variables
    ... $res = mysql_query; ... What I have discovered is re-using the $req variable is fine. ... using the $res produces undefined result (sometimes the second query ... In other words since PHP uses garbage collection re-using the same ...
    (comp.lang.php)
  • Re: re-using variables
    ... $res = mysql_query; ... In other words since PHP uses garbage collection re-using the same ...
    (comp.lang.php)
  • Re: Difference between mysql_affected_rows() and mysql_num_rows()
    ... // MyQuery runs mysql_query and die's if there was an error. ... $res = MyQuery; ... // Run the query which is encoded in $q ... [php made right http://code.google.com/p/pihipi] ...
    (comp.lang.php)
  • Re: php mysql result problem
    ... Jerry Stuckle wrote: ... insert res: 1 ... If you bracket it and simplify it ... The php manual which I am *sure* you know by heart Jerry, being such a 'professional' programmer and ubiquitous smartass and all, is fairly explicit: ...
    (comp.lang.php)