我正在尝试使用PDO在PHP中运行一个简单的查询。代码非常简单:
try {
$sql_query = "select * from Articles where title=':article'";
$dbh = get_PDO_connection();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare($sql_query);
$stmt->execute( array( ':article' => "MD Example") );
var_dump($stmt);
$row = $stmt->fetch();
//if($row==null)return 'null';
var_dump($row);
} catch (PDOException $e) {
print $e->getMessage ();
}
var_dump($stmt)
总是打印:
object(PDOStatement)#3 (1) { ["queryString"]=> string(45) "select * from Articles where title=':article'" }
无论我将什么内容放入
execute()
中,都不会抛出任何异常。正如标题所述,fetch()
总是返回false。请注意,如果我将SQL查询硬编码到字符串中,所有这些都可以正常工作。换句话说,如果我将$sql_query
设置为"select * from Articles where title='MD Example'",那么fetch()
将返回预期的结果。这里到底发生了什么?