developer tip

SQLSTATE [42000] : 구문 오류 또는 액세스 위반 : 1064 SQL 구문에 오류가 있습니다 — PHP — PDO

optionbox 2020. 12. 7. 08:04
반응형

SQLSTATE [42000] : 구문 오류 또는 액세스 위반 : 1064 SQL 구문에 오류가 있습니다 — PHP — PDO


동일한 문제가있는 다른 모든 StackOverflow (및 Google) 게시물을 살펴 봤지만 내 문제를 해결하는 것 같지는 않았습니다.

PDO와 PHP를 사용하고 있습니다.

내 코드 :

$vals = array(
   ':from'    => $email,
   ':to'      => $recipient,
   ':name'    => $name,
   ':subject' => $subject,
   ':message' = >$message
);
print_r($vals);
try {
   $pdo = new PDOConfig();
   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $sql = "SELECT * FROM messages WHERE `message` LIKE :message";
   $q = $pdo->prepare($sql);
   $q->execute(array(':message' => $vals[':message']));
   $resp = $q->fetchAll();

   foreach ($resp as $row) {
      throw new Exception('Please do not post the same message twice!');
   }

   $sql = "INSERT INTO messages (from, to, name, subject, message) VALUES (:from, :to, :name, :subject, :message)";
   $q = $pdo->prepare($sql);
   $q->execute($vals);
} 
catch(PDOException $e) {
   echo $e->getMessage();
}

첫 번째 print_r은

Array ( [:from]    => abc@gmail.com 
        [:to]      => lala@me.com 
        [:name]    => abc 
        [:subject] => abc 
        [:message] => abc )

예상되는 (none은 null이 아님)

그러나 그것은 오류를 출력합니다

SQLSTATE [42000] : 구문 오류 또는 액세스 위반 : 1064 SQL 구문에 오류가 있습니다. MySQL 서버 버전에 해당하는 설명서에서 'from, to, name, subject, message) VALUES ('abc@gmail.com', 'lala@me.com') 근처에서 사용할 올바른 구문을 확인하십시오.

이 문제를 해결하는 방법을 모릅니다. 어떤 아이디어?


from is a keyword in SQL. You may not used it as a column name without quoting it. In MySQL, things like column names are quoted using backticks, i.e. `from`.

Personally, I wouldn't bother; I'd just rename the column.

PS. as pointed out in the comments, to is another SQL keyword so it needs to be quoted, too. Conveniently, the folks at drupal.org maintain a list of reserved words in SQL.


I've got this exact error, but in my case I was binding values for the LIMIT clause without specifying the type. I'm just dropping this here in case somebody gets this error for the same reason. Without specifying the type LIMIT :limit OFFSET :offset; resulted in LIMIT '10' OFFSET '1'; instead of LIMIT 10 OFFSET 1;. What helps to correct that is the following:

$stmt->bindParam(':limit', intval($limit, 10), \PDO::PARAM_INT);
$stmt->bindParam(':offset', intval($offset, 10), \PDO::PARAM_INT);

Same pdo error in sql query while trying to insert into database value from multidimential array:

$sql = "UPDATE test SET field=arr[$s][a] WHERE id = $id";
$sth = $db->prepare($sql);    
$sth->execute();

Extracting array arr[$s][a] from sql query, using instead variable containing it fixes the problem.


ALTER TABLE `{$installer->getTable('sales/quote_payment')}`
ADD `custom_field_one` VARCHAR( 255 ) NOT NULL,
    ADD `custom_field_two` VARCHAR( 255 ) NOT NULL;

Add backtick i.e. " ` " properly. Write your getTable name and column name between backtick.

참고URL : https://stackoverflow.com/questions/4544051/sqlstate42000-syntax-error-or-access-violation-1064-you-have-an-error-in-you

반응형