2009-10-28 34 views
0

我有一個意外的T_ELSE在這個函數的最後一個else。小PHP函數中意外的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塊應該是自包含的,並且兩者都是封閉的,然後它應該去的時候,只有如果如果東西看起來腥?

回答

0

你已經得到雙重的} ..見下文

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

改變它

 $rows = array(); 

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

UPDATE ..

哎呀錯

其實你忘了加

if($con) { 
+0

我還有其他幾個函數,其中沒有一個需要if($ con){,所以我不認爲這是問題。 – 2009-10-28 15:04:41

1

有一個額外的支架某處......如果你縮進代碼,你會看到你沒有正確關閉各段...

您將需要之前,首先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組>錯誤將顯示任何東西......你需要mysql_error了點。

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); 
    } 
} 
+0

YOu已經綁定了我的代碼,但邏輯似乎相同,在相同的邏輯順序中有相同數量的括號。 – 2009-10-28 15:03:25

+0

查看評論。你有一個不必要的別的。 – erenon 2009-10-28 15:38:02

0

你最後還有是外面的功能括號,如:

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

您應該使用一個IDE,突出源,把光標依次對每個支撐,你會看到匹配的括號。