MySQL语法错误,我根本就看不出来

时间:2013-10-02 17:26:54

标签: 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();
  }

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

  

语法错误或访问冲突:1064 SQL语法中出错;查看与您的MySQL服务器版本对应的手册,以便在'condition,service,value,rated_by,date_rated'附近使用正确的语法。值1(1,5,5,5,5,3,'18','在第1行) “

如果有人可以发现我的语法错误,或者可能发现其他错误导致这种情况会很糟糕。谢谢你的帮助。

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分: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())";

答案 2 :(得分:3)

Condition是MySQL中的保留字。你应该总是在`table`和`column`名称中使用反引号(“`”)来避免这样的错误。看起来你在sql查询的末尾还有一个额外的)

List of Reserved Words