2

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

3 Answers 3

7
SELECT * FROM $table_name ORDER BY ID DESC
Sign up to request clarification or add additional context in comments.

Comments

3

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";

Comments

2

Take a look at the ORDER BY clause.

You are looking for a DESCENDING order:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.