I have this php getting rows from my database, however when I while loop the results out into divs on my site, it always misses out the first row it has retrieved, and I have no idea why.
$db = mysqli_connect('localhost', 'root', '', 'cdb')
or die('Error connecting');
if( isset($_REQUEST['page'])) {
$_SESSION['page'] = $_REQUEST['page'];
}else{
$_SESSION['page'] = 1;
}
$records_per_page = 8;
$where = substr($where, 0, -3);
$query = "SELECT * FROM cars
".$where."";
$result = mysqli_query($db, $query)
or die("Error in query: '$query'");
$row = mysqli_fetch_assoc($result);
$i = 0;
$start = ($_SESSION['page'] - 1) * $records_per_page;
$end = ($_SESSION['page']) * $records_per_page;
while($row = mysqli_fetch_assoc($result) and $i < $end)
{
$i++;
if( $i > $start )
{
echo'
<div class="result_main flip'.$i.' shadow2">
<div class="main_result_carname">
<h2><b>'.$row['make'].' '.$row['model'].'</b></h2>
</div>
<div class="main_result_carprice">
<h2><b>£'.$row['price'].'</b></h2>
</div>
</div>';
}
}
?>
Looking at it, I feel it should work fine, but always starts at the 2nd row, the only variable I can see which might affect this is $i. There no reason why it starting at 1 should be wrong, but anyway, making it -1 results in no records showing, and making it 0 makes it stay the same while making it -1 makes it start at the 3rd row, and making it 2 it starts at 2d row.
$wherecam from? (just out of interest)foreach ($_POST['carID'] as $carID) { $where = $where." carIndex = ".$carID." OR"; }There might be an easier way, but I post an array and need to apply the values in it to the where clause. The I have$where = substr($where, 0, -3);just before the query to get rid of the " OR" at the end.$where = "carIndex IN (".implode(',', $_POST['carID']).")"no need for substr.