Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL - get next record id from results

Posted on 2010-11-22
3
Medium Priority
?
690 Views
Last Modified: 2012-05-10
Hi,
I have the following query which returns a result set and loops through to display the different records. Is there a way to get the id of the record coming next, perhaps using mysql_data_seek?


mysql_select_db($database_conn_data, $conn_data);
$query_view_items = "SELECT * FROM items ORDER BY sortid";
$view_items = mysql_query($query_view_items, $conn_data) or die(mysql_error());
$row_view_items = mysql_fetch_assoc($view_items);



do {


//get current record id
$id = $row_view_items['id'];

//code here to push pointer forward by one

$next_id =  $row_view_items['id'];

//code here to move pointer back by one
......
...

} while ($row_view_items = mysql_fetch_assoc($view_items));

Thanks in Advance for your feedback.

Cheers,

Andrew
0
Comment
Question by:sabecs
  • 2
3 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34193888
$numrows = mysql_num_rows($result);
for ($i = 0; $i < $numrows; $i++) {
    if (!($row = mysql_fetch_assoc($result))) {
        continue;
    }
    if (!mysql_data_seek($result, $i + 1)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($nextrow = mysql_fetch_assoc($result))) {
        continue;
    }

    echo $row['last_name'] . ' ' . $row['first_name'] . "<br />\n";
}

Open in new window

0
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 2000 total points
ID: 34193961
Will try again - forgot a step.

numrows = mysql_num_rows($result);
for ($i = 0; $i < $numrows; $i++) {
    if (!mysql_data_seek($result, $i)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($row = mysql_fetch_assoc($result))) {
        continue;
    }
    if (!mysql_data_seek($result, $i + 1)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($nextrow = mysql_fetch_assoc($result))) {
        continue;
    }

}

Open in new window

0
 

Author Closing Comment

by:sabecs
ID: 34201258
Thanks for your help.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question