Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rows from table in different orders

Tags:

php

The code While($row=mysql_fetch_assoc($res)) return rows 1,2,3...n-1,n.

I need after to get from the same $res (not to make new query) the rows but in another order n,n-1,...,2,1.

It is possible?

like image 321
Michael Avatar asked Mar 13 '23 05:03

Michael


2 Answers

You can use mysqli_data_seek() function.

Example:

$result = mysqli_query( $query );
$totalRows = mysqli_stmt_num_rows( $result );

for( $i=($totalRows-1); $i>=0; $i-- )
{
    mysqli_data_seek( $result, $i );
    $row = mysqli_fetch_row( $result );
}

Please note: You can use mysqli_data_seek only after mysqli_query, mysqli_store_result or mysqli_use_result.


  • See more about mysqli_data_seek()
like image 79
fusion3k Avatar answered Mar 20 '23 12:03

fusion3k


Simple juste reverse array and save it in a new variable.

<?php

  $newvar=array_reverse($array);
 ?>
like image 44
Castro Alhdo Avatar answered Mar 20 '23 13:03

Castro Alhdo