作为新手,我遵循PHP MySQL教程,建议使用常规MySQL php函数。但是,由于我被告知PDO是更好的选择,我一直在将我的代码转换为该代码。我只是碰到了以下问题:MySQL PDO查询语法错误与?参数
$query = $uspdb->prepare("SELECT post_id, is_approved, reports FROM ? WHERE id=? AND ?");
$query->bindValue(1, $table, PDO::PARAM_INT);
$query->bindValue(2, $id, PDO::PARAM_INT);
$query->bindValue(3, checkPermission("comment_moderation"),PDO::PARAM_BOOL);
$query->execute;
$result = $query->fetch(PDO::FETCH_ASSOC);
第一行抛出以下PDO例外:
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 '? WHERE id=? AND ?' at line 1
这是为什么?我不知道语法有什么问题。 tutorial I'm reading告诉我应该使用bindValue或execute(array(stuff))来添加参数而不是“。$ id”。和类似的,因为它更安全,但这不是因为什么原因工作。
您将表名称绑定为'PDO :: PARAM_INT',相当肯定这是错误的。 –