Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reverse query result 1

Status
Not open for further replies.

gxydas

Programmer
Jan 15, 2005
76
0
0
GR
Hi all,
I have this code:

Code:
$query= "select * from table1";
$result= mysqli_query($db, $query);
@$num_results= mysqli_num_rows($result);

My question is how can i display the rows in reversed order.
I don't want to use 'order by DESC' in SQL statement.
I need to do that in PHP.
Any help?
 
Why do you need to do it in PHP?

Also, on your last line, the "@" character is in the wrong place, it should be before the "mysqli" function (if you really need it at all).

Anyway to answer your question. Get all the record, put them into an array and read the array from end to beginning.
Code:
$tmp = array();
while ($rw = mysqli_fetch_assoc($result))
   $tmp[] = $rw;
for ($i=count($tmp)-1;$i>-1;$i--) {
//
// do your processing using $tmp[$i]
//
}

Ken
 
Why not just add ASC or DESC to the end or your query so that it sorts it in the manner you want?
 
I solved my problem.
Thanks boys
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top