SQLSTATE[42000]: 语法错误或访问冲突: 1064 您的 SQL 语法中有错误 — PHP —磷酸二氢钾
我已经浏览了所有其他 StackOverflow(和谷歌)有同样问题的帖子,但似乎没有一个能解决我的问题。
我正在使用 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] => [email protected]
[:to] => [email protected]
[:name] => abc
[:subject] => abc
[:message] => abc )
预期的结果(没有一个为空),
但它输出错误
SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“from、to、name、subject、message”附近使用的正确语法 VALUES ('[电子邮件受保护]', '[电子邮件受保护]' 第 1 行
不知道如何解决此问题?
I've looked through all the other StackOverflow (and google) posts with the same problem, but none seemed to address my problem.
I am using PDO and PHP.
My code:
$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();
}
and the first print_r gives
Array ( [:from] => [email protected]
[:to] => [email protected]
[:name] => abc
[:subject] => abc
[:message] => abc )
which is expected (none are null)
but it outputs the error
SQLSTATE[42000]: 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 'from, to, name, subject, message) VALUES ('[email protected]', '[email protected]' at line 1
No idea how to fix this. any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
from
是 SQL 中的关键字。如果不引用它,则不能将其用作列名。在 MySQL 中,诸如列名之类的内容使用反引号引用,即`from`
。就我个人而言,我不会打扰;我只是重命名该列。
附言。正如评论中指出的,
to
是另一个 SQL 关键字,因此也需要用引号引起来。方便的是,drupal.org 的人员维护了一个 列表SQL 中的保留字。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.我遇到了这个确切的错误,但在我的例子中,我绑定了 LIMIT 子句的值,而没有指定类型。我只是将其放在这里,以防有人因同样的原因出现此错误。如果未指定类型
LIMIT :limit OFFSET :offset;
则会导致LIMIT '10' OFFSET '1';
而不是LIMIT 10 OFFSET 1;
。以下内容有助于纠正这一问题: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 typeLIMIT :limit OFFSET :offset;
resulted inLIMIT '10' OFFSET '1';
instead ofLIMIT 10 OFFSET 1;
. What helps to correct that is the following:正确添加反引号,即“`”。在反引号之间写下您的 getTable 名称和列名称。
Add backtick i.e. " ` " properly. Write your getTable name and column name between backtick.
尝试从多维数组插入数据库值时,sql 查询中出现相同的 pdo 错误:
从 sql 查询中提取数组
arr[$s][a]
,使用包含它的变量修复了问题。Same pdo error in sql query while trying to insert into database value from multidimential array:
Extracting array
arr[$s][a]
from sql query, using instead variable containing it fixes the problem.