php mysqli 查询结果显示所有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12647154/
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 03:57:33  来源:igfitidea点击:

mysqli query results to show all rows

phpmysqliwhile-loop

提问by Tom

I have the following code:

我有以下代码:

include $_SERVER['DOCUMENT_ROOT'].'/include/conn.php'; 

$query = "SELECT title FROM news_event";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
$row_cnt = $result->num_rows;
$result->free();
$mysqli->close();

This is fine if there is only one result as I can just echo $row['title']but if there are lots of results, how do I get this to loop through and print every row?

如果只有一个结果,这很好,因为我可以只回显,$row['title']但如果有很多结果,我如何让它循环并打印每一行?

I'm sure this is really simple but I'm just not sure what I need to search for in Google.

我确定这真的很简单,但我不确定我需要在 Google 中搜索什么。

I'm looking for a mysqliequivalent of this:

我正在寻找mysqli与此等效的内容:

while( $row = mysql_fetch_array($result) )
{
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
}

回答by raidenace

Just replace it with mysqli_fetch_arrayor mysqli_result::fetch_array:)

只需将其替换为mysqli_fetch_arraymysqli_result::fetch_array:)

while( $row = $result->fetch_array() )
{
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
}

Almost all mysql_*functions have a corresponding mysqli_*function.

几乎所有的mysql_*功能都有对应的mysqli_*功能。

回答by jaggedsoft

Simple mysqli solution:

简单的mysqli解决方案:

$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT * FROM table WHERE 1');
while ( $rows = $resource->fetch_assoc() ) {
    print_r($rows);//echo "{$row['field']}";
}
$resource->free();
$db->close();

With Error Handling:If there is a fatal error the script will terminate with an error message.

带错误处理:如果出现致命错误,脚本将终止并显示错误消息。

// ini_set('display_errors',1); // Uncomment to show errors to the end user.
if ( $db->connect_errno ) die("Database Connection Failed: ".$db->connect_error);
$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT field FROM table WHERE 1');
if ( !$resource ) die('Database Error: '.$db->error);
while ( $row = $resource->fetch_assoc() ) {
    echo "{$row['field']}";
}
$resource->free();
$db->close();

Using iterators:Support was added with PHP 5.4

使用迭代器:PHP 5.4 添加了支持

$db = new mysqli('localhost','user','password','database');
foreach ( $db->query('SELECT * FROM table') as $row ) {
    print_r($row);//echo "{$row['field']}";
}
$db->close();

Fetch a single record:This code does not require a loop.

获取单个记录:此代码不需要循环。

$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT field FROM table');
$row = $resource->fetch_assoc();
echo "{$row['field']}";
$resource->free();
$db->close();

回答by Stegrex

Use:

用:

while ($row = $result->fetch_array(MYSQLI_BOTH)) {
    // Look inside $row here, do what you want with it.
}

Look at the associative array examples here (should correspond with fetch_array() versions as well):

看看这里的关联数组示例(也应该与 fetch_array() 版本对应):

http://php.net/manual/en/mysqli-result.fetch-assoc.php

http://php.net/manual/en/mysqli-result.fetch-assoc.php

回答by Dharman

You can simply loop using foreach

您可以简单地循环使用 foreach

$query = "SELECT title FROM news_event";
$result = $mysqli->query($query);
foreach($result as $row) {
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
}

If you would like to keep the mysqli code and HTML separate, then it's good idea to fetch all into an array and loop later.

如果您希望将 mysqli 代码和 HTML 分开,那么最好将所有内容提取到一个数组中并稍后循环。

$query = "SELECT title FROM news_event";
$result = $mysqli->query($query);
$news_events = $result->fetch_all(MYSQLI_ASSOC);

// Later in HTML:    

<div>
<?php foreach($news_events as $row): ?>
    <p><?= $row['FirstName'] ?> <?= $row['LastName'] ?></p>
<?php endforeach ?>
</div>