Reputation: 2154
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
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
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