Sort sql output in ascending order

I’m trying to sort the output of SQL out in ascending order. but it doesn’t work. My code is like below

if($result->num_rows>0){
    while($row = $result->fetch_assoc()){
        print "<tr><td>" 
        . $row["id"] . "</td><td>" 
        . $row["firstname"] . "</td><td>" 
        . $row["lastname"] . "</td><td>" 
        . $row["mphone"] . "</td><td>" 
        . $row["hphone"] . "</td><td>" 
        . $row["email"] . "</td><td>" 
        . $row["address"] . "</td><td>" 
        . $row["city"] . "</td><td>"
        . $row["zip"] . "</td><td>" 
        . $row["state"] . "</td><td>" 
        . $row["country"] . "</td><td>" 
        . $row["age"] . "</td><td>"
        . $row["height"] . "</td><td>" 
        . $row["transport"] . "</td><td>" 
        . $row["day"] . "</td><td>" 
        . $row["gender"] . "</td><td>"
        . $row["message"] . "</td><tr>";
    }
}

SELECT * FROM mytable
ORDER BY Country DESC;

What happens if you change DESC to ASC?

Are you certain
Country === country ???

* I can only guess, “doesn’t work” by itself says essentially nothing to me.
Error messages and / or more detail about unexpected results would be more helpful

1 Like

Still shows error.

The data doesn’t show whenever I add the block below

SELECT * FROM mytable
ORDER BY Country DESC;

What do you see with the else bit added on

if($result->num_rows>0){
    while($row = $result->fetch_assoc()){
        print "<tr><td>" 
        . $row["id"] . "</td><td>" 
        . $row["firstname"] . "</td><td>" 
        . $row["lastname"] . "</td><td>" 
        . $row["mphone"] . "</td><td>" 
        . $row["hphone"] . "</td><td>" 
        . $row["email"] . "</td><td>" 
        . $row["address"] . "</td><td>" 
        . $row["city"] . "</td><td>"
        . $row["zip"] . "</td><td>" 
        . $row["state"] . "</td><td>" 
        . $row["country"] . "</td><td>" 
        . $row["age"] . "</td><td>"
        . $row["height"] . "</td><td>" 
        . $row["transport"] . "</td><td>" 
        . $row["day"] . "</td><td>" 
        . $row["gender"] . "</td><td>"
        . $row["message"] . "</td><tr>";
    }
} else {
echo '$result->num_rows is ' . $result->num_rows; 
}

Thank you so much. I’ll try it and get back to you

Nothing. no difference. Just shows the results

That sounds like the script is working.
If not, please explain.

@Mittineague Yes the script is working. I want it to be sorted by country, or any other field o fmy choice

Can you post your actual code? There must be something else in there which is causing you an issue…

Might it be they are sorting but not as you expect them to?

Does it look anything like this collation effect example?

https://dev.mysql.com/doc/refman/5.0/en/charset-collation-effect.html

I like this answer however I am not sure OP understood the meaning and corrected the issue.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.