0

我在这个函数的最后一个 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 有些东西看起来很可疑?

4

4 回答 4

1

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

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

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

于 2009-10-28T14:56:00.367 回答
0

你有两倍的} ..见下文

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

将其更改为

        $rows = array();

 } else if ($cmd == "Option2") 

更新 ..

哎呀错了

实际上你忘了添加

if($con) {
于 2009-10-28T14:55:18.637 回答
0

你的代码是错误的。整齐地组织你的代码。你错过了一个 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);
   }
}
于 2009-10-28T14:56:52.437 回答
0

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

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

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

于 2009-10-28T15:08:36.077 回答