0

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.

3
  • where does your $where cam from? (just out of interest) Commented Apr 13, 2014 at 23:39
  • $where = " WHERE "; 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. Commented Apr 13, 2014 at 23:43
  • missing sanitize aside... $where = "carIndex IN (".implode(',', $_POST['carID']).")" no need for substr. Commented Apr 14, 2014 at 20:55

1 Answer 1

3

Remove

$row = mysqli_fetch_assoc($result);

that is before your

$i = 0;

$result = mysqli_query($db, $query)
  or die("Error in query: '$query'");
$row = mysqli_fetch_assoc($result);
$i = 0;

That is selecting your 1st row and moving the internal pointer forward so

while($row = mysqli_fetch_assoc($result) and $i < $end)

will begin selecting from the 2nd row

Sign up to request clarification or add additional context in comments.

1 Comment

Awww Yisss Thank you, not sure why I put that in there, tis showing the first row now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.