如何使用 PDO 在 PHP 中获取结果数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10911757/
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
How to use PDO to fetch results array in PHP?
提问by Bundy
I'm just editing my search script after reading up on SQL injection attacks. I'm trying to get the same functionality out of my script using PDO instead of a regular mysql connection. So I've been reading other posts about PDO but am unsure. Will these two scripts give the same functionality?
在阅读了 SQL 注入攻击之后,我正在编辑我的搜索脚本。我正在尝试使用 PDO 而不是常规的 mysql 连接从我的脚本中获得相同的功能。所以我一直在阅读有关 PDO 的其他帖子,但我不确定。这两个脚本会提供相同的功能吗?
With PDO:
使用 PDO:
$pdo = new PDO('mysql:host=$host; dbname=$database;', $user, $pass);
$stmt = $pdo->prepare('SELECT * FROM auction WHERE name = :name');
$stmt->bindParam(':name', $_GET['searchdivebay']);
$stmt->execute(array(':name' => $name);
With regular mysql:
使用常规 mysql:
$dbhost = @mysql_connect($host, $user, $pass) or die('Unable to connect to server');
@mysql_select_db('divebay') or die('Unable to select database');
$search = $_GET['searchdivebay'];
$query = trim($search);
$sql = "SELECT * FROM auction WHERE name LIKE '%" . $query . "%'";
if(!isset($query)){
echo 'Your search was invalid';
exit;
} //line 18
$result = mysql_query($trim);
$numrows = mysql_num_rows($result);
mysql_close($dbhost);
I go on with the regular example to use
我继续使用常规示例
while($i < $numrows){
$row = mysql_fetch_array($result);
to create an array of matching results from the database. How do I do this with PDO?
从数据库创建匹配结果的数组。我如何用 PDO 做到这一点?
回答by Polynomial
Take a look at the PDOStatement.fetchAllmethod. You could also use fetchin an iterator pattern.
看一下PDOStatement.fetchAll方法。您也可以fetch在迭代器模式中使用。
Code sample for fetchAll, from the PHP documentation:
fetchAll, 来自 PHP 文档的代码示例:
<?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(\PDO::FETCH_ASSOC);
print_r($result);
Results:
结果:
Array
(
[0] => Array
(
[NAME] => pear
[COLOUR] => green
)
[1] => Array
(
[NAME] => watermelon
[COLOUR] => pink
)
)
回答by Your Common Sense
There are three ways to fetch multiple rows returned by PDO statement.
有三种方法可以获取 PDO 语句返回的多行。
The simplest one is just to iterate over PDOStatement itself:
最简单的方法就是迭代 PDOStatement 本身:
$stmt = $pdo->prepare("SELECT * FROM auction WHERE name LIKE ?")
$stmt->execute(array("%$query%"));
// iterating over a statement
foreach($stmt as $row) {
echo $row['name'];
}
another one is to fetch rows using fetch() method inside a familiar while statement:
另一种方法是在熟悉的 while 语句中使用 fetch() 方法获取行:
$stmt = $pdo->prepare("SELECT * FROM auction WHERE name LIKE ?")
$stmt->execute(array("%$query%"));
// using while
while($row = $stmt->fetch()) {
echo $row['name'];
}
but for the modern web application we should have our datbase iteractions separated from output and thus the most convenient method would be to fetch all rows at once using fetchAll() method:
但是对于现代 Web 应用程序,我们应该将我们的数据库迭代与输出分开,因此最方便的方法是使用 fetchAll() 方法一次获取所有行:
$stmt = $pdo->prepare("SELECT * FROM auction WHERE name LIKE ?")
$stmt->execute(array("%$query%"));
// fetching rows into array
$data = $stmt->fetchAll();
and then output them in a template:
然后在模板中输出它们:
<ul>
<?php foreach($data as $row): ?>
<li><?=$row['name']?></li>
<?php endforeach ?>
</ul>
Note that PDO supports many sophisticated fetch modes, allowing fetchAll() to return data in many different formats.
请注意,PDO 支持许多复杂的获取模式,允许 fetchAll() 以多种不同格式返回数据。
回答by rehan ali
$st = $data->prepare("SELECT * FROM exampleWHERE example LIKE :search LIMIT 10");

