Jump to content

Archived

This topic is now archived and is closed to further replies.

chester600

"ORDER BY" help

Recommended Posts

Hey all!

I am quite new to php/MySQL and have hit a problem during learning them.

I have created a table in my database that holds a user_id and the users age, which looks like this:

[table]
[tr][td][b]user_id[/b][/td][td][b]age[/b][/td][/tr]
[tr][td]1[/td][td]56[/td][/tr]
[tr][td]2[/td][td]70[/td][/tr]
[tr][td]3[/td][td]17[/td][/tr]
[tr][td]4[/td][td]67[/td][/tr]
[tr][td]5[/td][td]72[/td][/tr]
[tr][td]6[/td][td]60[/td][/tr]
[tr][td]7[/td][td]99[/td][/tr]
[tr][td]8[/td][td]100[/td][/tr]
[tr][td]9[/td][td]98[/td][/tr]
[tr][td]10[/td][td]200[/td][/tr]
[/table]

Now when I use this query in php:

[code]$query = "SELECT user_id, age FROM members ORDER BY age DESC";[/code]

I get the following output:
[table]
[tr][td]user_id[/td][td]age[/td][/tr]
[tr][td]7[/td][td]99[/td][/tr]
[tr][td]9[/td][td]98[/td][/tr]
[tr][td]5[/td][td]72[/td][/tr]
[tr][td]3[/td][td]70[/td][/tr]
[tr][td]4[/td][td]67[/td][/tr]
[tr][td]6[/td][td]60[/td][/tr]
[tr][td]1[/td][td]56[/td][/tr]
[tr][td]10[/td][td]200[/td][/tr]
[tr][td]2[/td][td]17[/td][/tr]
[tr][td]8[/td][td]100[/td][/tr]
[/table]

For some reason 200 is being treated as < 56 and 100 as < 17. Anyone have any ideas why this would be????

Thanks in advance!

Chester600  :)

Share this post


Link to post
Share on other sites

Try this but yours should of worked.
[code]
$query = "SELECT user_id, age FROM members where age <= 200 ORDER BY age DESC";
[/code]

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=105859.msg423002#msg423002 date=1156717562]
What filed type is age?
[/quote]

It is char.

Thanks for your reply redarrow but that still gives the same output  :(

Share this post


Link to post
Share on other sites
[quote author=redarrow link=topic=105859.msg423007#msg423007 date=1156717980]
change char to int then try your statement.
[/quote]

Brilliant!!! Worked perfectly.....thanks  ;D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.