当前位置: 动力学知识库 > 问答 > 编程问答 >

php - stmt->execute not working properly

问题描述:

I am trying to get a result from the below piece of code, however, no result is being returned. $stmt->execute(array(id)) is not running as expected. It seems to work fine in other pieces of code, and I have compared them and don't see any differences.

$stmt = $mysql->dbh->prepare("SELECT * FROM keys WHERE type = ?");

if($stmt->execute(array($id))) {

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

echo $row["price"];

}

}

Thanks

网友答案:

keys is a MySQL reserved word

  • http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Either you wrap it in backticks or use another name for it.
Just don't rename it to key. That too is a MySQL reserved word.

SELECT * FROM `keys`

Using error exception would have thrown you an error.

Add setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION) right after the connection is opened, if you're not already doing so.

Consult: http://php.net/manual/en/pdo.error-handling.php

  • Assuming you have already established a successful DB connection.
分享给朋友:
您可能感兴趣的文章:
随机阅读: