简体   繁体   中英

Why does this simple PDO prepared statement won't work?

$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);

Gives exception:

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

Tried to put the parameters in the execute function, same message.

PDO options i've set are:

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

group保留字 ,在MariaDB中,如果没有反引号,则不能用作标识符

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

group 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 with back-ticks, ie :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM