Results 1 to 8 of 8

Thread: [RESOLVED] to add "desc" into mysql_query but it dont take affect

  1. #1

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Resolved [RESOLVED] to add "desc" into mysql_query but it dont take affect

    hi
    I am newbee to this category, my problem is;
    I have mysql_query that work fine so far, now I want to add "desc" into mysql_query but it dont take affect, the result just as it was without "desc".

    i know "desc" can make it in decsending order say 1 to 9, then with "desc" it become 9 to 1

    my query is this

    $sql = mysql_query("SELECT * FROM comments WHERE tutorialid='$tutid' ORDER BY tutorialid DESC LIMIT $from, $max_results")or die(mysql_error();

    thanks for advice, your suggestion is very much appreciated


  2. #2
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: to add "desc" into mysql_query but it dont take affect

    I think your ORDER BY... DESC should come at the very end...

    Code:
    $sql = mysql_query("SELECT * FROM comments WHERE tutorialid='$tutid'  LIMIT $from, $max_results ORDER BY tutorialid DESC")or die(mysql_error();

  3. #3

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: to add "desc" into mysql_query but it dont take affect

    BlackRiver,
    Thanks for your help

    when I put order by... at the end it is an error like this

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY tutorialid DESC' at line 1

    Code:
    $sql = mysql_query("SELECT * FROM comments WHERE tutorialid='$tutid' LIMIT $from, $max_results ORDER BY tutorialid DESC")or die(mysql_error();
    Regards,
    cesin

  4. #4

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: to add "desc" into mysql_query but it dont take affect

    you should be sorting by "commentid" or something, rather than tutorialid. tutorialid is the ID of the tutorial, and you're specifically selecting only one tutorialid ($tutid). I don't know what your table looks like, so I can't really be any more specific than that.

  6. #6
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: to add "desc" into mysql_query but it dont take affect

    Your first code is fine,I made a mistake.

    MySQL pays no attention to the DESC in the ORDER BY if you order by the primary key and also include a simple equals where condition on an indexed column.

    A note has been added to the 5.1.22 and 5.0.48 changelogs:

    When sorting rows in an INNODB table using a primary key, where the sort was on the the
    primary key column and the DESC operator was applied, the rows would be incorrectly
    sorted. if you included a simple WHERE field = value clause in the query.

  7. #7
    Junior Member
    Join Date
    Jun 2009
    Posts
    21

    Re: to add "desc" into mysql_query but it dont take affect

    Why are you using a string datatype for numeric values if you want to sort them ?

    And you are trying to sort a list of values that are the same

    SELECT * FROM comments WHERE tutorialid='$tutid' ORDER BY tutorialid

    so if $tutid = 5 you get only records with tutorialid = 5 and sort those fives.

  8. #8

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: to add "desc" into mysql_query but it dont take affect

    yes , I should use commentid as indexing, it work. thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width