order by problem

Discussion in 'PHP' started by Greenmethod, Aug 29, 2007.

  1. #1
    I have a statement...

    $desc_result = mysql_query("SELECT * FROM categories WHERE main_cat='$main_cat' AND sub_cat_1='$sub_cat_1' ORDER BY desc ASC");
    PHP:
    It works if I take out the "order by" part, but not if I leave it in. I KNOW that I have a field in the database that is called desc, and I have another query under this one that works fine, with the order by. Any suggestions?

    Thanks in advance!
     
    Greenmethod, Aug 29, 2007 IP
  2. sabmalik

    sabmalik Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    "ORDER BY desc ASC"

    whats that for? u hava a field named desc?

    if yes then use it as `desc` (those are backticks not single qoutes).
     
    sabmalik, Aug 29, 2007 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try putting backticks around your column and table names:
    $desc_result = mysql_query("SELECT * FROM `categories` WHERE `main_cat`='$main_cat' AND `sub_cat_1`='$sub_cat_1' ORDER BY `desc` ASC"

    I would assume it's getting confused by the fact that 'desc' is the opposite of 'asc' and that it just thinks that you haven't defined a column name... hence the reason you should use backticks.
     
    TwistMyArm, Aug 29, 2007 IP
  4. sabmalik

    sabmalik Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    elaborating on the above a bit further...

    backticks must be used whenever u have mistakenly used a mysql keyword as column name. in this case it is desc ...... if u were to do a "select desc from tablename" , u would get an error .... if u would do "select `desc` from tablename" then it would work ..... consider the backtick as an escape character for mysql keywords.
     
    sabmalik, Aug 29, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Eek. "consider the backtick as an escape character for mysql keywords" is an interesting way to put it...

    The backtick is the correct way to refer to columns and tables when using MySQL. The fact that MySQL accepts queries without it is OK, but when you start using MySQL setups that are set to 'strict' then you will have problems again.

    So do yourself a favour: learn when (and when not!) and where to use backticks and normal single quotes. Trust me... you will save yourself a lot of headaches!
     
    TwistMyArm, Aug 29, 2007 IP
  6. sabmalik

    sabmalik Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    TwistMyArm , thanks for elaborating that .... i was just trying to explain it in easy terms for his newbie mind :)
     
    sabmalik, Aug 29, 2007 IP
  7. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for all your help! desc is a column in my table, and it didn't even occur to me that it could be a mysql function!
     
    Greenmethod, Aug 30, 2007 IP