Jump to content

Trouble sorting numbers in a mysql database *Resolved*


DarkHorizon

Recommended Posts

I have a database containing products, each has a price.

i want to be able to sort the database by price, high to low or low to high.


$query = "SELECT * FROM $table order by price asc LIMIT $limitvalue, $limit";

$limitvalue and $limit are pagination variables which work okay

the table gets sorted by price okay but i get:

1.50
17.00
19.50
2.00
22.00
234.00
3.19

you can see whats happening, wbut why? and how can i fix it display the correct ascending order.
Link to comment
Share on other sites

Why does it sort like that? Because you are storing the prices as strings not numbers and that sort is correct for strings. Change your field type to something like FLOAT(6,2) instead of VARCHAR or whatever you're using.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.