How to get next/previous record in MySQL & PHP?
My example is to get the id only
function btn_prev(){
$id = $_POST['ids'];
$re = mysql_query("SELECT * FROM table_name WHERE your_id < '$id' ORDER BY your_id DESC LIMIT 1");
if(mysql_num_rows($re) == 1)
{
$r = mysql_fetch_array($re);
$ids = $r['your_id'];
if($ids == "" || $ids == 0)
{
echo 0;
}
else
{
echo $ids;
}
}
else
{
echo 0;
}
}
function btn_next(){
$id = $_POST['ids'];
$re = mysql_query("SELECT * FROM table_name WHERE your_id > '$id' ORDER BY your_id ASC LIMIT 1");
if(mysql_num_rows($re) == 1)
{
$r = mysql_fetch_array($re);
$ids = $r['your_id'];
if($ids == "" || $ids == 0)
{
echo 0;
}
else
{
echo $ids;
}
}
else
{
echo 0;
}
}