Solved

MySQL - get next record id from results

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

785 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