Symfony DBAL插入给出:“语法错误或访问冲突:1064”


Symfony DBAL insert gives: "Syntax error or access violation: 1064"

我试图插入一些数据到数据库中,但有些东西在这里。我不知道它是什么,我希望有人能告诉我这是怎么回事。

我的表看起来有点像这样:

CREATE TABLE IF NOT EXISTS `db_name`.`order` (
  `orderID` INT NOT NULL AUTO_INCREMENT,
  `order_ordernumber` VARCHAR(200) NOT NULL,
  `order_orderweight` INT NOT NULL,
  ... And other columns as well, but all NULL
ENGINE = InnoDB

我使用Symfony2-framework,它是dbl -insert这里:

$conn->insert('order', array(
              'order_ordernumber' => $this->orderid,
              'order_orderweight' => $this->totalweight
             ));

"$this->orderid"为字符串变量,"$this->totalweight"为整型。

它给出了这个错误信息:

An exception occurred while executing 'INSERT INTO order (order_ordernumber, order_orderweight) VALUES (?, ?)' with params ["000001", 900]:
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 'order (order_ordernumber, order_orderweight) VALUES ('000001', '900')' at line 1
500 Internal Server Error - DBALException
1 linked Exception: PDOException » 

我可以做同样的查询纯sql和它的工作,这一个没有。这家伙到底是怎么回事?

order是MySQL的保留关键字:http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html

你必须使用反引号来防止这个错误。但是更好的建议是给你的表加上前缀。