【问题标题】:Unexpected T_ELSE in small PHP function小型 PHP 函数中出现意外的 T_ELSE
【发布时间】:2010-12-10 21:36:03
【问题描述】:

我在这个函数的最后一个 else 上有一个意外的 T_ELSE。

function QueryPeople($stringQuery, $table, $max, $cmd) {
    $con = mysqli_connect("localhost","user","password", "host");

    if ($cmd == "Option1") {
        $SearchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE lower(signature) LIKE ?" . $max;

        if ($fetchData = $con->prepare($SearchSQL)) {
            $fetchData->bind_param("s", "%".$stringQuery."%");
            $fetchData->execute();
            $fetchData->bind_result($signature, $firstname, $birthdate);
            $rows = array();
        }
    } else if ($cmd == "Option2") {
        $searchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE birthdate = ?" . $max;

        if ($fetchData = $con->prepare($searchSQL)) {
            $fetchData->bind_param(":birthdate", $stringQuery);
            $fetchData->execute();
            $fetchData->bind_result($signature, $firstname, $birthdate);
            $rows = array();
        }
    }

    while ($fetchData->fetch()) {
        $row = array(
            'signature' => $signature,
            'firstname' => $firstname,
            'birthdate' => $birthdate,
            );
            $rows[] = $row;
    }
    return $rows;
} else {                   // <-- This else doesn't have an if
    print_r($con->error);  // <-- This else doesn't have an if
}                          // <-- This else doesn't have an if
}

我真的无法理解为什么会这样。两个 if 块都应该是自包含的,并且都是封闭的,然后它应该去 while,并且只有 if if 有些东西看起来很可疑?

【问题讨论】:

    标签: php semantics parentheses


    【解决方案1】:

    你有双倍的 } .. 见下文

            $rows = array();
     } 
     } else if ($cmd == "Option2") 
    

    改成

            $rows = array();
    
     } else if ($cmd == "Option2") 
    

    更新 ..

    错了

    其实你忘了加

    if($con) {
    

    【讨论】:

    • 我还有其他几个函数,都不需要 if($con) {,所以我认为这不是问题。
    【解决方案2】:

    某处有一个额外的括号...如果您缩进代码,您会发现您没有正确关闭每个部分...

    您需要在第一个 if 之前添加一个 if($con)

    function QueryPeople($stringQuery, $table, $max, $cmd) {
        $con = mysqli_connect("localhost","user","password", "host");
    
        if($con){
            if ($cmd == "Option1") {
    
                $SearchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE lower(signature) LIKE ?" . $max;
    
                if ($fetchData = $con->prepare($SearchSQL)) {
                    $fetchData->bind_param("s", "%".$stringQuery."%");
                    $fetchData->execute();
                    $fetchData->bind_result($signature, $firstname, $birthdate);
                    $rows = array();
                } 
            } else if ($cmd == "Option2") {
    
                $searchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE birthdate = ?" . $max;
    
                if ($fetchData = $con->prepare($searchSQL)) {
                    $fetchData->bind_param(":birthdate", $stringQuery);
                    $fetchData->execute();
                    $fetchData->bind_result($signature, $firstname, $birthdate);
                    $rows = array();
                } 
    
            } 
    
            while ($fetchData->fetch()) {
                $row = array(
                'signature' => $signature,
                'firstname' => $firstname,
                'birthdate' => $birthdate,
                );
                $rows[] = $row;
            }
            return $rows;
        } else {
            print_r($con->error);
        }
    }
    

    无论如何,我认为 $con->error 不会显示任何内容...您需要mysql_error

    【讨论】:

      【解决方案3】:

      您的代码错误。整齐地组织你的代码。你错过了一个 if 语句。

      <?php
      function QueryPeople($stringQuery, $table, $max, $cmd) {
        //
      
      if ($cmd == "Option1") {
      
        //
      
          if ($fetchData = $con->prepare($SearchSQL)) {
           /**/
          } 
      
      } else if ($cmd == "Option2") {
      
          //
          if ($fetchData = $con->prepare($searchSQL)) {
              /**/
          } 
      
      } 
          while ($fetchData->fetch()) {
              /**/
         }
            return $rows;
          } else {        //<- WHAT else?
              print_r($con->error);
         }
      }
      

      【讨论】:

      • 你绑定了我的代码,但逻辑似乎相同,相同逻辑顺序中的括号数相同。
      • 查看评论。你有一个不必要的 else。
      【解决方案4】:

      您的最后一个 else 在函数大括号之外,例如:

      function funcName() {
         // function body...
      } else {
         // this is where your else is
      }
      

      您应该使用高亮显示源代码的 IDE,将光标依次放在每个大括号上,您将看到匹配的大括号。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-05
        • 1970-01-01
        • 2012-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        相关资源
        最近更新 更多