mildog8
mildog8

Reputation: 2154

reverse mysql database results with php

I have am grabbing a whole lot of data from a table in a mysql database and displaying it on a page. The code looks like this.

<?php
$sql = "SELECT * FROM $table_name";
$result = mysql_query($sql);
while($rows = mysql_fetch_array($result)){
?>
<tr class="contact-content">
    <td><?php echo $rows['ID']; ?></td>
    <td><?php echo $rows['name']; ?></td>
    <td><?php echo $rows['email']; ?></td>
    <td><?php echo $rows['tel_home']; ?></td>
    <td><?php echo $rows['tel_mobile']; ?></td>
    <td><?php echo $rows['tel_work']; ?></td>
</tr>
<?php
}
?>

It currently displays results like this

1 | James | [email protected] | 1234567 | 1234567 | 9876

2 | Anna | [email protected] | 8768765 | 6543 | 9876

But I would like to reverse it and display the results something like this ordered by there id

2 | Anna | [email protected] | 8768765 | 6543 | 9876

1 | James | [email protected] | 1234567 | 1234567 | 9876

Upvotes: 2

Views: 1391

Answers (3)

Mike Mackintosh
Mike Mackintosh

Reputation: 14237

Take a look at the ORDER BY clause.

You are looking for a DESCENDING order:

$sql = "SELECT * FROM $table_name ORDER BY ID DESC";

Upvotes: 2

Jocelyn
Jocelyn

Reputation: 11393

Use one of theses queries:

Order by ascending name:

$sql = "SELECT * FROM $table_name ORDER BY name ASC";

Order by descending ID:

$sql = "SELECT * FROM $table_name ORDER BY ID DESC";

Upvotes: 3

gronke
gronke

Reputation: 189

SELECT * FROM $table_name ORDER BY ID DESC

Upvotes: 7

Related Questions