Jump to content

[SOLVED] ORDER BY not listing correctly


timmah1

Recommended Posts

I'm selecting a person's worth from the database, I need to list them from highest to lowest.

 

These are the numbers in the database as of right now

20609

5480

9587

 

My code is this

$result = mysql_query("SELECT * FROM profile ORDER BY worth ASC");
$i = 1;

 

Then to display them

while($row = mysql_fetch_array($result)) { 
$number = $row['worth'];
$format_number = number_format($number, 2, '.', ',');
echo $i; $format_number;
++$i;
}

 

The problem I'm having is that it lists the numbers like they are above, not in order from highest to lowest

20609

5480

9587

 

If I change it to DESC, it displays like this

9587

5480

20609

 

Can anybody see what the problem is?

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/111607-solved-order-by-not-listing-correctly/
Share on other sites

It looks as if the field type for worth is a text field and not a numeric field. If you order those values as if they were text then the results you are getting is correct: a 2 comes before a 5 just as B comes before F. Change the field type of "worth" to an appropriate numeric field.

 

EDIT: You also have several unnecessary lines. No reason to create the variable $number just to use it one on the next line:

 

<?php

$query = "SELECT * FROM profile ORDER BY worth ASC";
$result = mysql_query($query) or die (mysql_error()."<br>$query");
$i = 1;

while($row = mysql_fetch_array($result)) { 
  echo $i++ . number_format($row['worth'], 2, '.', ',');
}

?>

 

And whay are you not following the advice you are including in your own sig?

Archived

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

×
×
  • Create New...

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.