I am querying a mysql database table by first creating a temporary table and filling it with data from a select statement.
To test it,i am using a known database id and test if it gets the next and previous records correctly.
This are my functions
function get_next($id){
$db = new PDO('mysql:host=localhost;dbname=world', 'root', '');
$db->query("CREATE TEMPORARY TABLE nr AS SELECT ID,Name,Population FROM city WHERE id > $id ORDER BY ID LIMIT 1;");
$orm = $db->query("SELECT * FROM nr WHERE ID > $id ORDER BY ID LIMIT 1;");
$id = $orm->fetchColumn(0);
if ($id !== false) {
return $id;
}
}
function get_previous($id){
$db = new PDO('mysql:host=localhost;dbname=world', 'root', '');
$db->query("CREATE TEMPORARY TABLE pr AS SELECT ID,Name,Population FROM city WHERE ID > $id ORDER BY ID LIMIT 1;");
$orm = $db->query("SELECT * FROM pr WHERE ID < $id ORDER BY ID DESC LIMIT 1;");
$id = $orm->fetchColumn(0);
if ($id !== false) {
return $id;
}
}
I can get the next
record correctly but i get previous
record wrong as it says the previous record is the current record.
Am i doing the previous query wrong?.