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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14491430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:23:37  来源:igfitidea点击:

Using PDO to echo display all rows from a table

phppdo

提问by Michael N

I'm trying to echo out all the rows of a table using PDO but am running into trouble.

我正在尝试使用 PDO 回显表的所有行,但遇到了麻烦。

With the old way of doing I'd have done it like

用旧的方式做我会这样做

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
   $title= $row['title'];
   $body= $row['body'];
}

But with PDO I'm trying;

但是我正在尝试使用 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;

Which keeps giving me Call to undefined method PDO::fetchAll()

这一直让我调用未定义的方法 PDO::fetchAll()

Doing the example given in the manual

执行手册中给出的示例

<?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);
?>

Works, but I don't think I have control over the individual colums like I would with a $row=['blah']; do I? It also prints out like this; rather ugly:

有效,但我认为我无法像使用 $row=['blah']; 那样控制各个列。我呢?它也像这样打印出来;比较丑:

Array ( [0] => Array ( [title] => This is the test title entered in the database[0]

Array ( [0] => Array ( [title] => 这是数据库中输入的测试标题[0]

What needs to be done to properly use PDO to do this?

需要做什么才能正确使用 PDO 来做到这一点?

回答by Green Black

change:

改变:

while ($row = $db->fetchAll(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}

to:

到:

while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}

回答by Fabian Schmengler

Which keeps giving me Call to undefined method PDO::fetchAll()

这一直让我调用未定义的方法 PDO::fetchAll()

This should have given you the hint, that you are using the wrong object. It's PDOStatement::fetchAllas you can see in your second example, or if you want to use it in a while loop PDOStatement::fetch:

这应该给你提示,你使用了错误的对象。它是PDOStatement::fetchAll,如您在第二个示例中所见,或者如果您想在 while 循环中使用它PDOStatement::fetch

while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
    $title = $row['title'];
    $body = $row['body'];
}

Additional notes:

补充说明:

  1. $resultis a misleading variable name as you might see from the $result->execute()line. You don't execute a result, you execute a statement. This is why in the manual $stmtor $sth(statement handle i guess) are used.
  2. The echolines should be inside the while loop, otherwise you overwrite again and again, then output only the last row.
  1. $result是一个误导性的变量名称,您可能会从该$result->execute()行看到。您不执行结果,而是执行语句。这就是为什么在手册$stmt$sth(我猜是语句句柄)中使用的原因。
  2. 这些echo行应该在while循环内,否则你一次又一次地覆盖,然后只输出最后一行。