2013-10-02 56 views
2

我正在執行一條sql語句的語法錯誤,但對於我的生活我找不到任何錯誤。MySQL語法錯誤,我根本看不到在哪裏

我已經創建了自己的數據庫類,並具有功能來完成預處理語句並執行它們,我認爲我的實現沒有問題,因爲我之前從未遇到任何問題。但是,爲了防止出現問題,我也將顯示相應的代碼。

準備:

public function prepare($index, $sql) { 
    if(isset(self::$PS[$index])){ 
     $ermsg = "Index [$index] is already in use."; 
     throw new Exception($ermsg, 1); 
    } 
    try{ 
     self::$PS[$index] = $this->dbh->prepare($sql); 
    } 
    catch(PDOException $e){ 
     return false; 
    } 
    return true; 
} 

,並執行:使用此,所以我不認爲這是問題

public function execute($index, Array $param = array()) { 
    if(!isset(self::$PS[$index])){ 
     $ermsg = "Index [$index] is unavailable."; 
     throw new Exception($ermsg, 1); 
    } 

    foreach($param as $key => $val){ 
     if(is_int($key)) ++$key; 

     $type = $this->getValueType($val); 

     $bnd = self::$PS[$index]->bindValue($key, $val, $type); 

     if(!$bnd){ 
      $ermsg = "Paramater '$key' in [$index] failed to bind"; 
      throw new Exception($ermsg, 2); 
     } 

    } 

    try{ 
     $bnd = self::$PS[$index]->execute(); 
    } 
    catch(PDOException $e){ 
     $ermsg = "PDO-Error while executing prepared statement [$index] ".$e->getMessage(); 
     throw new Exception($ermsg, 3); 
    } 

    if($bnd === false){ 
     $ermsg = "Result error in prepared statement [$index]"; 
     throw new Exception($ermsg, 3); 
    } 

    return self::$PS[$index]; 
} 

正如我所說的,我從來沒有經歷過的問題,但誰知道。

現在我實現:

$sql = "INSERT INTO ratings (course_id, overall, design, condition, service, value, rated_by, date_rated) 
     VALUES (:course_id, :overall, :design, :condition, :service, :value, :rated_by, now()))"; 

    DBH::getInstance()->prepare('rateit', $sql); 


     $stmt = DBH::getInstance()->execute('rateit', array(
      ":course_id"=>$course_id, 
      ":overall"=>$overall, 
      ":design"=>$design, 
      ":condition"=>$condition, 
      ":service"=>$service, 
      ":value"=>$value, 
      ":rated_by"=>$log_user_id 
    )); 
    } 
    if($stmt) { 
     echo 'suc, Thanks! Your ratings have been added to the course.'; 
     exit(); 
    }else{ 
     echo 'err, There was an error rating the course. Please try again later'; 
     exit(); 
    } 

這是確切的語法錯誤消息我收到:

語法錯誤或訪問衝突:1064您的SQL語法錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在'condition,service,value,rated_by,date_rated'附近使用正確的語法)VALUES(1,5,5,5,3,'18','at line 1 「

如果有人能發現我的語法錯誤,或者可能找到別的錯誤,將導致此這將是真棒。感謝您的幫助。

+4

結束還有一個額外的)'condition'是在MySQL的保留字。您可以更改列名稱,也可以使用反引號引用它。 – andrewsi

+5

它看起來像在now()附近有一個額外的')'。 – Brian

+0

看看這個:http://stackoverflow.com/q/1563146/1267304 – DontVoteMeDown

回答

7

條件是MySQL中的reserved word。這可能是。它

+2

那*是*它... –

+0

是的!好主,不能相信我發佈了所有這些代碼大聲笑。謝謝,這成爲一個非常頭痛的問題。 – codeguerrilla

3
$sql = "INSERT INTO ratings (course_id, overall, design, condition, service, value, rated_by, date_rated) 
    VALUES (:course_id, :overall, :design, :condition, :service, :value, :rated_by, now()))"; 

應該是:

$sql = "INSERT INTO ratings (course_id, overall, design, `condition`, service, value, rated_by, date_rated) 
    VALUES (:course_id, :overall, :design, :condition, :service, :value, :rated_by, now())"; 
+0

你改變了什麼? –

+0

你也需要引用'condition'。 – andrewsi

+2

now()))「; - > now())」; – bksi

2

Condition是MySQL中的一個保留字。您應該總是在`table`和`column`名稱中使用反引號(「`」)以避免類似的錯誤。也像你在SQL查詢

List of Reserved Words

+1

同意。沒有評論的downvote是可怕的。我已+1了抵消,加上你回答OP的問題。 – Brian