我试图用PDO回显表的所有行,但遇到了麻烦.
用旧的做法,我已经做到了
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
$title= $row['title'];
$body= $row['body'];
}
但是我正在尝试使用PDO;
$result = $db->prepare("SELECT title, body FROM post");
$result->execute();
while ($row = $db->fetchAll(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}
echo $title;
echo $body;
哪个一直给我调用未定义的方法PDO :: fetchAll()
做手册中给出的例子
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
工作,但我不认为我可以控制单个列,就像我用$row = [‘blah’];我呢?它也像这样打印出来;相当难看:
Array ( [0] => Array ( [title] => This is the test title entered in the database[0]
要正确使用PDO来做这件事需要做些什么?