为什么这个简单的PDO准备好的语句不起作用?

时间:2019-03-09 10:44:53

标签: php pdo mariadb prepared-statement

$stmt =$dbh->prepare('SELECT * FROM config WHERE group=:group AND name=:name');
$stmt->bindParam(':group',$group, PDO::PARAM_STR);
$stmt->bindParam(':name',$name, PDO::PARAM_STR);

给出例外:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'group=? AND name=?' at line 1

试图将参数放在execute函数中,相同的消息。

我设置的PDO选项是:

PDO::ATTR_ERRMODE=> PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => FALSE,

2 个答案:

答案 0 :(得分:1)

groupreserved word,在MariaDB中没有反引号的情况下不能用作identifier

SELECT * FROM `config` WHERE `group`= :group AND `name` = :name

答案 1 :(得分:1)

group是SQL中的关键字。您必须在未引用的情况下将其用作列名。在MySQL中,列名之类的内容用反引号引起来,即:

$stmt =$dbh->prepare('SELECT * FROM config WHERE `group` = :group AND `name` = :name');