为什么我的查询不执行?

时间:2012-10-27 10:00:56

标签: php mysql sql pdo

我有一个查询可以获取这些变量(但是来自POST表单):

$username = "John";
$email = "johnsmith@mail.com";
$passwordEnc = "9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684";
$activated = 0;
$activationKey = "5753a498f025464d72e088a9d5d6e872592d5f91";

查询如下:

$stmt = $dbu->prepare("INSERT INTO users (username, email, password, activated, key) VALUES (?, ?, ?, ?, ?)");
$stmt->execute(array($username, $email, $passwordEnc, $activated, $activationKey));

没有错误,但该条目未添加到名为“users”的表中。

3 个答案:

答案 0 :(得分:6)

您需要使用反引号passwordkey转义reserved words in MySQL

INSERT INTO users (username, email, `password`, activated, `key`) VALUES ...

答案 1 :(得分:2)

我检查了http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html,只是为了节省一些时间'Key'是唯一保留在那里的字。

答案 2 :(得分:0)

$dbu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbu->errorInfo();

添加此行以获取任何pdo查询错误