PHP:为什么我不能在 mysqli_fetch_array() 结果上循环两次?

2023-03-05php开发问题
3

本文介绍了PHP:为什么我不能在 mysqli_fetch_array() 结果上循环两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在从 mysql 数据库中检索一些数据.有两个循环使用相同的结果集.

I'm retrieving some data from a mysql database. There are two loops which uses the same result set.

while ($row = mysqli_fetch_array($newsQuery)) {
     echo "<a href='news-article.php?articleId=" .$row["news_id"]."' class="list-group-item active">".$row["news_title"]."</a>";
}

这个循环以成功结束.然后在同一页面中,我有以下循环.

This loop ends with success. then in the same page I have the following loop.

while ($row = mysqli_fetch_array($newsQuery)) {
    echo $row["news_content"];
}

即使表中有内容,此循环也不会返回任何内容.当我在第一个循环中尝试时.内容显示正确.知道我做错了什么.

This loop doesn't return anything even if there are content in the table. When I try it in the first loop. the content is displayed correctly. Any idea on what I'm doing wrong.

推荐答案

来自 PHP 的 mysqli_fetch_array DOCS:

From PHP's mysqli_fetch_array DOCS:

返回一个与获取的行相对应的数组,如果 result 参数表示的结果集没有更多行,则返回 NULL.

Returns an array that corresponds to the fetched row or NULL if there are no more rows for the resultset represented by the result parameter.

您在 $row = mysqli_fetch_array($newsQuery)

这意味着循环将继续进行,直到 mysqli_fetch_array($newsQuery) 返回 NULL.

This means the loop will keep going untill mysqli_fetch_array($newsQuery) returns NULL.

这就是您不能再次使用该循环的原因,因为 mysqli 已完成获取结果并且 mysqli_fetch_array($newsQuery) 现在返回 NULL 直到您进行新查询.

This is the reason why you cant use that loop again, since mysqli has finished fetching the results and the mysqli_fetch_array($newsQuery) now returns NULL untill you make a new query.

尝试先用结果填充一个变量,然后在该变量上循环:

Try populating a variable with the results first, then loop on that variable:

$results = array();
while ($row = mysqli_fetch_array($newsQuery)) {
     $results[] = $row;
}

foreach ($results as $key => $row) {
    echo "<a href='news-article.php?articleId=" .$row["news_id"]."' class="list-group-item active">".$row["news_title"]."</a>";
}


foreach ($results as $key => $row) {
    echo $row["news_content"];
}

这篇关于PHP:为什么我不能在 mysqli_fetch_array() 结果上循环两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17