I have two queries that should return the next and previous rows in my DB relative to the current row.
However they both return the same thing, the next row, and for some reason the 2nd query also returns an empty data set.
Code:
<?php
// Credentials
$dbhost = "localhost";
$dbname = "buildingcodes";
$dbuser = "***";
$dbpass = "***";
global $tutorial_db;
$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");
// Check Connection
if ($tutorial_db->connect_errno)
{
printf("Connect failed: %s\n", $tutorial_db->connect_error);
exit();
}
$id = 'R401.4 Soil tests.'; //$_GET['id'];
echo 'id: ' . $id;
echo '</br>';
//////
$query = "SELECT * FROM codes WHERE subsection > '". $id ."' ORDER BY subsection LIMIT 1";
$result = $tutorial_db->query($query);
while($results = $result->fetch_array())
{
$result_array[] = $results;
}
echo '</br></br>';
if (isset($result_array))
{
foreach ($result_array as $result)
{
echo 'first q: ' . $result['subsection'];
echo '</br>';
}
}
/////
$query = "SELECT * FROM codes WHERE subsection < '". $id ."' ORDER BY subsection LIMIT 1";
$result = $tutorial_db->query($query);
while($results = $result->fetch_array())
{
$result_array[] = $results;
}
echo '</br>';
if (isset($result_array))
{
foreach ($result_array as $result)
{
echo 'second q: ' . $result['subsection'];
echo '</br>';
}
}
?>
Outputs:
id: R401.4 Soil tests.
first q: R401.4.1 Geotechnical evaluation.
second q: R401.4.1 Geotechnical evaluation.
second q:
But should output:
id: R401.4 Soil tests.
first q: R401.4.1 Geotechnical evaluation.
second q: R401.3 Drainage.
From this set of data:
Why is this not returning the previous row?