我正在尝试使用 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()
执行手册中给出的示例
<?php
$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] => 这是数据库中输入的测试标题[0]
需要做什么才能正确使用 PDO 来做到这一点?