using order by and limit gives error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #1

    using order by and limit gives error

    hi i have written a query and it is giving me th following error

    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 ' 2' at line 6
    SELECT * FROM pretzel_product s P , pretzel_product s_description PD WHERE (P.prod_id=PD.p rod_id and prod_status<>'0 ') ORDER BY prod_date_added DESC LIMIT , 2
    here is my query ,
    any suggesstions
    [code=mysql]
    $q="SELECT * FROM
    ".PRODUCT." P ,
    ".PRODUCT_DESCR IPTION." PD
    WHERE
    (P.prod_id=PD.p rod_id and prod_status<>'0 ')
    ORDER BY prod_date_added DESC AND LIMIT $offset , $limit ";
    $r=execute($q);
    [/code]
    Last edited by Atli; Dec 6 '09, 01:36 PM. Reason: Changed the [b] tags to [quote] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You are missing the offset in your LIMIT clause.
    The manual lists it as:
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [code=sql]
    -- Yours is like:
    ... LIMIT , 2

    -- It needs to be either
    ... LIMIT 1, 2

    -- Or just
    ... LIMIT 2

    -- Can also be
    ... LIMIT 2 OFFSET 1[/code]

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      So basically, the error is that your variable, $offset, is empty.

      Also, there is an inconsistency between the queries being shown. In your code, you have an "AND" between the ORDER BY clause and the LIMIT clause... That is invalid syntax.

      Comment

      • omerbutt
        Contributor
        • Nov 2006
        • 638

        #4
        yeah that AND was a typo mistake and basically the error was that the offset variable was empty
        due to which the error was occuring
        hanks
        regards,
        Omer Aslam

        Comment

        Working...