Results 1 to 6 of 6

Thread: mysql query error ...

  1. #1
    Join Date
    Nov 2008
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default mysql query error ...

    Hello:

    I am having a problem fixing/understanding a recent error message. Basically, I have a search form that after validation and filtering stores the variable into a session. Another page/file opens and loads that search field. Everything has always worked but I recently added pagination and suddenly this program no longer works. Any assistance would greatly be appreciated.


    php version=5.3.1
    mysql version=5.1.41


    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 'ASC LIMIT 0,10' at line 1


    Pagination:
    http://www.catchmyfame.com/2007/07/2...ination-class/



    Here is the shortened version of the code:


    include('../pagination/paginator.php');
    $searcher = $_SESSION['sessearchcode'];

    $query = "SELECT COUNT(*) FROM tblLoad, tblCode WHERE (tblLoad_LoadID = tblCode_CodeID) AND tblCode_Manufact LIKE '%$searcher%'";
    $result = mysql_query($query) or die(mysql_error());
    $num_rows = mysql_fetch_row($result);

    $pages = new Paginator;
    $pages->items_total = $num_rows[0];
    $pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
    $pages->paginate();

    $query = "SELECT tblLoad_LoadID, tblLoad_Company, tblLoad_State, tblCode_CodeID from tblLoad, tblCode WHERE (tblLoad_LoadID = tblCode_CodeID) AND tblCode_Manufact LIKE '%$searcher%' ASC $pages->limit";
    $result = mysql_query($query) or die(mysql_error());


    Thank You.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you'd have to show us an example of the output SQL statement to be sure, but it looks like you're using ASC without an order by clause:
    Code:
    SELECT whatever FROM sometable WHERE col LIKE 'searchterm' ORDER BY some_col ASC LIMIT 0,10
    also, since ASC is the default, you don't really need to specify it. Simply using "ORDER BY" will produce the same result.
    Last edited by traq; 09-21-2011 at 06:55 PM.

  3. #3
    Join Date
    Mar 2007
    Location
    Dublin, Ireland
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Here is the problem, as @traq explained:

    $query = "SELECT tblLoad_LoadID, tblLoad_Company, tblLoad_State, tblCode_CodeID from tblLoad, tblCode WHERE (tblLoad_LoadID = tblCode_CodeID) AND tblCode_Manufact LIKE '%$searcher%' ASC $pages->limit";

    You may remove the ASC completely as you probably don't need an ORDER BY clause.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by Butterfly View Post
    You may remove the ASC completely as you probably don't need an ORDER BY clause.
    I wouldn't go so far as to say that without knowing more about what the OP is trying to do. Not ordering your results typically returns rows in the order they were saved - which may or may not be what they want, but would almost certainly give a different result than ordered results would.

    (also, pepe_lepew, please note my edited post above.)

  5. #5
    Join Date
    Nov 2008
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Yeah, I was missing the ORDER BY.
    Thanks

  6. #6
    Join Date
    Mar 2007
    Location
    Dublin, Ireland
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by traq View Post
    I wouldn't go so far as to say that without knowing more about what the OP is trying to do. Not ordering your results typically returns rows in the order they were saved - which may or may not be what they want, but would almost certainly give a different result than ordered results would.
    Correct, I was only explaining the origin of the error.

Bookmarks

Posting Permissions

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