0

I'm just wondering how I could fetch data using a while loop at row 5 in my SQL table and end it at row 8.

Here is an example of my current code:

while($row = $results->fetch(PDO::FETCH_ASSOC))
                        {
                        echo '
                        <li>Mileage: '.number_format($row["Mileage"]).'</li>
                        <li>Engine size: '.$row["EngineSize"].'cc</li>
                        ';} ?>

So as you can see I'd like to make it so the while loop only loops from row 5 to row 8 so I'd be left with 4 li's with the unique data in those columns. As it stands it is currently looping every row in the SQL table which is what I don't want.

Any code examples would be great as I am really stuck on this at the moment. Thanks.

Dale Doback
  • 97
  • 2
  • 11
  • The answer from Sujit is most likely what you want but an answer to the literal question (start looping an array at a specific index): http://stackoverflow.com/questions/795625/how-to-set-an-arrays-internal-pointer-to-a-specific-position-php-xml – castis Sep 15 '14 at 21:25

1 Answers1

1

MySQL developers and SQL designers must have actually been able to predict this question years back. :)

LIMIT clause comes to your rescue

http://www.mysqltutorial.org/mysql-limit.aspx

Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79