SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误— PHP — PDO [英] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDO

查看:134
本文介绍了SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误— PHP — PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了所有其他Stackoverflow(和google)中相同的问题,但似乎都没有解决我的问题.

I've looked through all the other StackOverflow (and google) posts with the same problem, but none seemed to address my problem.

我正在使用PDO和PHP.

I am using PDO and 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给出

and the first print_r gives

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

这是预期的(没有一个为空)

which is expected (none are null)

但输出错误

SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有一个错误;请参阅第1914页的"SQLSTATE错误".请查看与您的MySQL服务器版本相对应的手册,以获取正确的语法,以在从,到,名称,主题,消息"附近使用VALUES("abc@gmail.com","lala@me.com",位于第1行)

不知道如何解决此问题.有什么想法吗?

No idea how to fix this. any ideas?

推荐答案

from是SQL中的关键字.如果不引用,则不能将其用作列名.在MySQL中,列名之类的内容用反引号引起来,即`from`.

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.如注释中所指出的,to是另一个SQL关键字,因此也需要将其引起来.方便地,drupal.org上的人员维护一个列表SQL中的保留字.

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.

这篇关于SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误— PHP — PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆