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.
'developer tip' 카테고리의 다른 글
Linux에서 인터페이스의 IP 주소 가져 오기 (0) | 2020.12.07 |
---|---|
Ubuntu 9.10에서 openCV를 확인하는 방법 (0) | 2020.12.07 |
Chrome 확장 프로그램 아이콘을 클릭 할 때마다 스크립트 실행 (0) | 2020.12.07 |
Silverlight와 유사한 WPF의 유효성 검사 오류 스타일 (0) | 2020.12.07 |
HQL없이 테이블의 모든 행을 검색합니까? (0) | 2020.12.07 |