Solved

MySQL - get next record id from results

Posted on 2010-11-22
3
651 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 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
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 …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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