MySQL语法错误,我根本看不到在哪里


MySQL syntax ERROR, I simply don't see where

我正在执行的sql语句有语法错误,但在我的一生中,我找不到任何错误。

我已经创建了自己的数据库类,具有制作预准备语句并执行它们的功能,我认为我的实现没有问题,因为我以前从未遇到过任何问题。但是,以防万一那里有问题,我也将展示代码。

准备:

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 服务器版本对应的手册,了解在"条件、服务、值、rated_by date_rated"附近使用的正确语法值(1、5、5、5、5、5、3、'18',在第 1 行'

如果有人能发现我的语法错误,或者发现其他错误会导致这种情况,那就太棒了。感谢您的任何帮助。

条件是 MySQL 中的一个保留字。可能就是这样。

根据 9.2 架构对象名称,要使用保留字作为标识符,必须将它们括起来。使用反引号,或者在启用 ANSI_QUOTES SQL 模式的情况下使用双引号。如果保留字跟在限定名称中的句点后面,则不需要引用。

$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())";

Condition是MySQL中的一个保留字。您应该始终在"表"和"列"名称中使用反引号 ("'"( 以避免此类错误。看起来您在 sql 查询结束时还有一个额外的)

保留字列表