简体   繁体   English

PHP中的嵌套MySQL查询

[英]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: 所需术语:

  • d($var) - Value assigned to $var or initialized. d($ var)-分配给$ var或初始化的值。
  • r($var) - Value of $var read. r($ var)-读取$ var的值。
  • u($var) - Value of $var is undefined. u($ var)-$ var的值未定义。

We wish to avoid the following patterns: 我们希望避免以下模式:

  • ur - A undefined value is read. ur-读取未定义的值。
  • du - A variable which once had a value becomes undefined before it's read. du-曾经具有值的变量在读取之前变得不确定。
  • dd - A variable receives a new value before it's earlier value is properly used; dd-变量在正确使用之前的值之前会收到一个新值; this is probably what you're afraid of. 这可能是您所担心的。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM