gpt4 book ai didi

php - 使用 PDO 回显显示表中的所有行

转载 作者:可可西里 更新时间:2023-11-01 13:43:12 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 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] => This is the test title entered in the database[0]

正确使用 PDO 需要做什么?

25 4 0