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

标签 php mysql sql

我正在执行的 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();
  }

这是我收到的确切的语法错误消息:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, service, value, rated_by, date_rated) VALUES (1, 5, 5, 5, 5, 3, '18',' at line 1'

如果有人能发现我的语法错误,或者可能发现其他导致此问题的错误,那就太棒了。感谢您的帮助。

最佳答案

条件是 reserved word在 MySQL 中。可能就是这样。

根据 9.2 Schema Object Names , 要使用保留字作为标识符,它们必须被引用。使用反引号或启用 ANSI_QUOTES SQL模式,双引号。如果保留字在限定名称中跟在句点之后,则不需要引号。

关于php - MySQL 语法错误,我根本看不到哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19143046/

相关文章:

java - PL/SQL 中的值检查列表为 NULL

sql - 你能在 SQL 中定义 "literal"表吗?

php - 论坛响应错误: Duplicate entry for PRIMARY key

mysql - 如何使用连接器/C++ 连接到远程 ipv6 mysql 服务器

Mysql 表结构运行速度快吗?

SQLFiddle:必须声明标量变量错误

php - 你如何在 category.tpl 文件 OpenCart 2 中调用辅助函数

php - 多维数组

php - 我想在 php 中将文件从本地主机上传到我的服务器

php - Laravel从数据库返回的日期时间与同一数据库中的记录不同