php – How fetch 1 more but get rid of it later? – Education Career Blog

Currently I fetch rows with the help of mysql_fetch_array() (and the LIMIT/OFFSET) (ie. while ($row = mysql_fetch_array($res)) { ... }); but the last row is not needed as it was to be fetched in order to make sure there is at least one more row left – it is not for display.


You can simply get the number of rows before this loop:

select count(*) from <your_table> WHERE <your_condition>;

Then, you can exclude the last record within the loop using a counter.
Is this what you want??


You should not mix presentation with logic anyway. Fetch all the rows in an array:

$rows = array();

while ($row = mysql_fetch_array($res)) {
    $rows = $row;   

and delete the last one:


Or just set LIMIT to get one less of that is possible.


Here is another possibility using a for loop and mysql_num_rows:

for($i = mysql_num_rows($res) - 1; $i--; $row = mysql_fetch_array($res))

but the first one is more readable imo.

Leave a Comment