如何获取mysql结果数据集内的下一条记录? [英] How to get next record inside the mysql result dataset?

查看:455
本文介绍了如何获取mysql结果数据集内的下一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mysql给我们一个数据集当我们在mysql中运行select查询时,我们使用while或for循环语句从这个数据集中获取每个记录。我想知道当我们得到一个记录而不到达循环的顶部时,是否有任何函数来获取该数据集中的下一个记录?
如下:

Mysql gives us a dataset When we run a select query in mysql and we use while or for loop statement to get each record from this dataset. I want to know is there any function to get the next record inside this dataset when we get a record without reach the top of the loop? something like:

$result = mysql_query("SELECT * FROM table1");
while ($row = mysql_fetch_array($result)) {

 $currentId = $row['id'];
 $nextId = mysql_fetch_next()['id'];
 ......

}

推荐答案

我将添加另一个迭代,将您的资源转换为数组,并包含下一个ID(假设它不一定是前一个ID + 1,在这种情况下这是无意义的):

I would add another iteration that converts your resource into an array and also includes the next ID (assuming it's not necessarily the previous ID + 1, in which case this is all pointless):

$recordset = array(); $i = 0;
while ($row = mysql_fetch_assoc($result))
{
    $recordset[$i] = $row;
    if ($i > 0) { $recordset[$i-1]['nextid'] = $row['id']; }
    $i++;
}

然后,您可以使用$ recordset作为任何目的。

Then you can use $recordset for whatever purpose.

这篇关于如何获取mysql结果数据集内的下一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆