Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 46

Thread: paginate search results

  1. #11
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    You have set a limit in your search query on line 42:

    Code:
            $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
    The value of '$per_page' is set to 5 in line 19 so that query only returns five results! If you want to return all the matching results starting from '$start_from' then you must change LIMIT to OFFSET. Your query should look like this:

    Code:
            $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id OFFSET $start_from");
    See here for the usage of LIMIT and OFFSET in MYSQL.

  2. #12
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I just modified the query but gave the error below when I clicked search

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'OFFSET 0' at line 1' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:45 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(45): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 45

    line 45 is
    Code:
    $exec = $result->execute();
    shall I paste the whole code again?

  3. #13
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Hmmm, it doesn't seem to like an OFFSET of 0. Try this in your query:

    Code:
    OFFSET $start_from+1

  4. #14
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Sorry I modified the query and now got this error

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'OFFSET 0+0' at line 2' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:46 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(46): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 46

    On line 46 is
    Code:
    $exec = $result->execute();
    the query looks like
    Code:
    $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id 
    		OFFSET $start_from+0");

  5. #15
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    First try:

    Code:
    $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id OFFSET $start_from+1");
    and if that doesn't work try:

    Code:
    $startxx = $start_from+1;
    $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id OFFSET $startxx");

  6. #16
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I tried the first way and got the following error

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'OFFSET 0+1' at line 1' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:45 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(45): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 45

    So then tried the second way and got the following error

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'OFFSET 1' at line 1' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:46 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(46): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 46

  7. #17
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    I've had a scour through Google and it looks as though OFFSET on its own is invalid. The only way to get all the records from a particular start point will be to set an impossibly large value for number of records retrieved. Like this:

    Code:
    $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, 1000000");

  8. #18
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by styxlawyer View Post
    I've had a scour through Google and it looks as though OFFSET on its own is invalid. The only way to get all the records from a particular start point will be to set an impossibly large value for number of records retrieved. Like this:

    Code:
    $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, 1000000");
    This worked but lost all the results, it does not paginate them to 5 per page for example?

  9. #19
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Without access to your live page, I can only guess what's going on.

  10. #20
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Sorry I did not mean lost all the records, I meant it displays all the records on one page insteading of paginate them to 5 per page, sorry I can't give access to the live page as has customer data on and our own business info, sorry

Similar Threads

  1. customising a maps with search results
    By jelly46 in forum Other
    Replies: 0
    Last Post: 11-08-2011, 12:53 AM
  2. Replies: 0
    Last Post: 12-28-2009, 09:14 AM
  3. PHP code to retrieve search results
    By GeordieD in forum PHP
    Replies: 1
    Last Post: 01-16-2009, 10:02 PM
  4. Replies: 0
    Last Post: 04-06-2008, 07:57 PM
  5. Split Search results into columns
    By e1seix in forum PHP
    Replies: 1
    Last Post: 07-15-2007, 10:11 PM

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
  •