"select * from table name where id>$id limit 1;"
bad bad naughty bad, this just get's the first row where id>$id, which does not have to be the row you want. (the database does not have to store the ID's in ascending order on disk!)
Instead:
SELECT MIN(id) as nextid
FROM table
WHERE id>$id
and then
SELECT *
FROM table
WHERE id = nextid
"select * from table_name where id< $id limit 0,1;"
a total guess methinks? :-)
SELECT MAX(id) as previousid
FROM table
WHERE id<$id
and then
SELECT *
FROM table
WHERE id = previousid