Go to next/previous record mysql

Here is an example:

<?php
$con = mysqli_connect(“localhost”,”root”,””,”vehicle”);
// Check connection
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

if (isset($_POST[‘id’])) {
$tmp = array_keys($_POST[‘id’]);
$curid = intval($tmp[0]);

// Select contents from the selected id
$sql = “SELECT * FROM cars WHERE id={$curid}”;
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result)>0) {
$info = mysqli_fetch_assoc($result);
} else {
die(‘Not found’);
}

// Next id
$sql = “SELECT id FROM cars WHERE id>{$curid} LIMIT 1”;
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_assoc($result)) {
$nextid = $row[‘id’];
}
}

// Prev id
$sql = “SELECT id FROM cars WHERE id<{$curid} LIMIT 1″;
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_assoc($result)) {
$previd = $row[‘id’];
}
}
} else {
// No form has been submitted so use the lowest id and grab its info
$sql = ” SELECT * FROM cars WHERE id > 0 LIMIT 1″;
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result)>0) {
$info = mysqli_fetch_assoc($result);
}
}

if (isset($info)) {
$content = ‘<pre>’.print_r($info,true).'</pre>’;
} else {
$content =  ‘Nothing in the db :(‘;
}
?>
<html>
<head>
<title>Next prev</title>
</head>
<body>
<h3>Info</h3>
<?php echo $content; ?>
<form method=”post”>
<?php if (isset($previd)) { ?>
<input type=”submit” name=”id[<?php echo $previd?>]” value=”prev”>
<?php } ?>
<?php if (isset($nextid)) { ?>
<input type=”submit” name=”id[<?php echo $nextid?>]” value=”next”>
<?php } ?>
</form>
</body>
</html>

Source: http://board.phpbuilder.com/showthread.php?10352339-go-to-next-record-mysql