php 如何遍历mysql结果集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1756743/
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 loop through a mysql result set
提问by sassy_geekette
What are some different ways to loop through a mysql result set? I'm new to PHP and MySQL so I'm looking for simple ways to loop through and an explanation as to how the provided code works.
循环遍历 mysql 结果集有哪些不同的方法?我是 PHP 和 MySQL 的新手,所以我正在寻找简单的循环方法以及有关所提供代码如何工作的解释。
采纳答案by Palantir
Here is a full example:
这是一个完整的例子:
http://php.net/manual/en/mysqli-result.fetch-array.php
http://php.net/manual/en/mysqli-result.fetch-array.php
- Connect
- Select database
- Make query
- Cycle on the result and fetch array to get the row
- 连接
- 选择数据库
- 进行查询
- 循环结果并获取数组以获取行
回答by Gabriel Sosa
the first example that comes to my mind:
我想到的第一个例子:
<?php
$link = mysql_connect(/*arguments here*/);
$query = sprintf("select * from table");
$result = mysql_query($query, $link);
if ($result) {
while($row = mysql_fetch_array($result)) {
// do something with the $row
}
}
else {
echo mysql_error();
}
?>
回答by Rahul Mukati
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8mb4');
$sql = "SELECT * FROM table";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);
foreach ($result as $row):
?>
//Loop Content... Example:-
<li><?= $row['name']; ?></li>
<?php
endforeach;
?>
回答by ax.
If you are using MySQL versions 4.1.3 or later, it is strongly recommendedthat you use the mysqliextension instead [of the mysqlextension that is not further developed, does not support features of MySQL 4.1+, no prepared and multiple statements, has no object-oriented interface, ...]
如果您使用的是 MySQL 4.1.3 或更高版本,强烈建议您使用mysqli扩展而不是[没有进一步开发的mysql扩展,不支持 MySQL 4.1+ 的特性,没有准备和多语句,有没有面向对象的接口,...]
see mysqli-stmt.fetchfor the procedural and object oriented ways to loop over a mysqli result set.
有关循环遍历 mysqli 结果集的过程和面向对象的方法,请参见mysqli-stmt.fetch。
回答by Kzqai
I'd recommend creating a database function that acts as a wrapper for your database fetch. Makes it easier to switch out database function calls, and even, down the road, the type of database itself (e.g. mysql->postgresql or mysql->couchdb or using the PDO object or something).
我建议创建一个数据库函数,作为数据库提取的包装器。使切换数据库函数调用变得更容易,甚至可以更轻松地切换数据库本身的类型(例如 mysql->postgresql 或 mysql->couchdb 或使用 PDO 对象或其他东西)。
Some function that -you- create that takes a query and returns a fully associative array, and then you stick the database connection code inside there.
您创建的某个函数接受查询并返回一个完全关联的数组,然后将数据库连接代码粘贴在其中。
It may also be good to check into using PDOdown the road, since it abstracts away database specific functions for you, working with mysql, postgresql, etc.
以后检查一下使用PDO也可能很好,因为它为您抽象了数据库特定的功能,使用 mysql、postgresql 等。

