Link to home
Start Free TrialLog in
Avatar of sabecs
sabecs

asked on

MySQL - get next record id from results

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
Avatar of sweetfa2
sweetfa2
Flag of Australia image

$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

ASKER CERTIFIED SOLUTION
Avatar of sweetfa2
sweetfa2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sabecs
sabecs

ASKER

Thanks for your help.