Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL syntax ERROR, I simply don't see where

Tags:

sql

php

mysql

I am having a syntax error with an sql statement I am executing but for the life of me I can't find any error.

I have created my own database class with functionality to make prepared statements and execute them, I don't think there is a problem with my implementation as I have never had any issues with it before now. But, just in case there is a problem there I am going to show the code for that too.

Prepare:

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;
}

and execute:

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];
}

As I mentioned, I have never experienced issues using this so I don't think it's the problem, but who knows.

Now my implementation:

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

This is the exact syntax error message I receive:

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'

If someone can spot my syntax error, or maybe find something else wrong that would be causing this that would be awesome. Thanks for any help.

like image 206
codeguerrilla Avatar asked Dec 12 '22 10:12

codeguerrilla


2 Answers

Condition is a reserved word in MySQL. That could be it.

According to 9.2 Schema Object Names, to use reserved words as identifiers they must be quoted. Use backticks or, with enabled ANSI_QUOTES SQL mode, double quotation marks. Quoting is not necessary if the reserved word follows a period in a qualified name.

like image 114
Machavity Avatar answered Dec 28 '22 16:12

Machavity


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

Should be:

$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())";
like image 33
Brian Avatar answered Dec 28 '22 14:12

Brian