How to get next record inside the mysql result dataset?

守給你的承諾、 提交于 2019-12-11 09:47:02

问题


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'];
 ......

}

??


回答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++;
}

Then you can use $recordset for whatever purpose.




回答2:


$result = mysql_query("SELECT MAX(id) FROM `table1` GROUP BY id");

if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_array($result)){
        $next = $row['MAX(id)'] + 1;
    }
}


来源:https://stackoverflow.com/questions/9599542/how-to-get-next-record-inside-the-mysql-result-dataset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!