重用变量 [英] re-using variables
问题描述
大家好,
我有以下代码:
$ req =" SELECT * FROM table1";
$ res = mysql_query($ req);
如果(!$ res)
返回;
while($ line = mysql_fetch_array($ res))
{
.............
.............
}
$ req =" SELECT * FROM table2";
$ res = mysql_query($ req);
if(!$ res)
return;
while($ line = mysql_fetch_array($ res))
{
.............
.............
}
我发现的是重新使用$ req变量很好。虽然使用$ res返回
会产生未定义的结果(有时第二个查询
工作正常,有时它不会。当我使用不同的变量时
就像$ res1和$ res2一样,mysql查询总是成功。如果变量包含一个
资源,那么初始
重新使用变量的方法是错误的(引用类型) )?
换句话说,因为PHP使用垃圾收集重新使用相同的
变量会导致问题。这是真的吗?
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?
推荐答案
req =" SELECT * FROM table1";
req = "SELECT * FROM table1";
res = mysql_query(
res = mysql_query(
req);
if(!
req);
if(!
这篇关于重用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!