sql query order by desc not ordering properly. PLZ HELP

Discussion in 'Databases' started by bigpapa, Dec 10, 2008.

  1. #1
    I have the following code:

    $popular = mysql_query("SELECT hits, link FROM links ORDER BY hits DESC LIMIT 0,20");
    
    while ($row = mysql_fetch__array($popular) {
    echo $row[link];
    }
    Code (markup):
    It does fetch 20 links from the row, which are supposed to be sorted by hits from the most hits to the least hits.

    Only, it is not sorting the results properly. For example, a link with 35 hits will appear under a link with only 6 hits.

    Can someone PLEASE help me figure out why this is happening????

    Thank you!!
     
    bigpapa, Dec 10, 2008 IP
  2. wootty

    wootty Peon

    Messages:
    447
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    0
    #2
    is 'hits' defined as an integer or numeric column in the table, or is it a char/varchar?
     
    wootty, Dec 10, 2008 IP
    bigpapa likes this.
  3. bigpapa

    bigpapa Banned

    Messages:
    273
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Its varchar(255)
     
    bigpapa, Dec 10, 2008 IP
  4. bigpapa

    bigpapa Banned

    Messages:
    273
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    +rep for you... I guess that was my problem. I changed it to integer and it works great now. When set as varchar, it took a number 9 and put it before 85, since the first number was 9.

    Thanks for pointing that out to me, I really aprpeciate it.
     
    bigpapa, Dec 10, 2008 IP