[英]Nested MySQL query in PHP
I came across the following code while constructing a php file: 在构建php文件时遇到了以下代码:
$query="UPDATE EXPERTS SET ...";
$result=$db->query($query);
if ($result)
{
$query="UPDATE USERS SET ...";
$result=$db->query($query);
if ($result)
{
...
}
}
My question is if the above nested query is acceptable since we use the same variable ($result). 我的问题是上述嵌套查询是否可以接受,因为我们使用了相同的变量($ result)。 Of course, it works, however I am not sure if is there any case of creating unexpected results...
当然可以,但是我不确定是否会产生意想不到的结果...
Thank you 谢谢
You can use Data Flow Anomalies for this: 您可以为此使用数据流异常 :
Required terminology: 所需术语:
We wish to avoid the following patterns: 我们希望避免以下模式:
Now let's take a look: 现在让我们看一下:
$query="UPDATE EXPERTS SET ...";
$result=$db->query($query); d($result)
if ($result) r($result)
{
$query="UPDATE USERS SET ...";
$result=$db->query($query); d($result)
if ($result) r($result)
{
...
}
}
You got two dr so we can safely assume that you got nothing to worry about; 您有两名医生,因此我们可以放心地假设您没有什么可担心的。 in both cases the values are assigned and read before being assigned again.
在这两种情况下,均会先分配并读取值,然后再重新分配。
One might argue that you should make a single variable which's purpose is to indicate the result of a single query but that's up to you. 有人可能会争辩说,您应该创建一个变量,其目的是指示单个查询的结果,但这取决于您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.