Sam Rosin
Sam Rosin

Reputation: 3

Properly Displaying Sorted SQL Rows

I am trying to sort my SQL database alphabetically by location. I executed the command: SELECT * FROM users ORDER BY location on the phpMyAdmin website, and the users are now indeed ordered alphabetically by location on the online database. However they still don't display in alphabetical order on our website. Here is the code where I output the relevant SQL data:

<div>
  <table id="checkintable">
    <tr>
        <th>Name</th>
        <th>Location</th>
        <th>Comment</th>
    </tr>                  
  <? $result = mysql_query("SELECT fullname, location, comment FROM users"); ?>
  <? while ($row = mysql_fetch_array($result)): ?>
    <tr class="tables">
        <td><?= $row["fullname"]?></td>
        <td><?= $row["location"]?></td>
        <td><?= $row["comment"]?></td>
    </tr>
    <br>
  <? endwhile ?>
  </table>
</div>

Am I doing something wrong in this code? I want to display the table in the same order that the online database is ordered in, but it isn't working.

Upvotes: 0

Views: 43

Answers (2)

Michał Powaga
Michał Powaga

Reputation: 23183

Change your:

mysql_query("SELECT fullname, location, comment FROM users")

to:

mysql_query("SELECT fullname, location, comment FROM users ORDER BY location")

Upvotes: 1

Otiel
Otiel

Reputation: 18743

You are missing the ORDER BY location in your query:

$result = mysql_query("SELECT fullname, location, comment FROM users ORDER BY location");

Upvotes: 3

Related Questions