0

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.

1
  • 1
    I don't understand why you're missing "order by location". You even mention that you used it from the mysql command line. Commented Dec 7, 2011 at 7:50

2 Answers 2

3

You are missing the ORDER BY location in your query:

$result = mysql_query("SELECT fullname, location, comment FROM users ORDER BY location");
Sign up to request clarification or add additional context in comments.

Comments

1

Change your:

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

to:

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

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.