Solved

PDO Paging

Posted on 2015-02-24
5
178 Views
Last Modified: 2015-02-25
Hi Experts,

I'm using PDO with Parametrized Queries, but how do I do paging?

Ex: from 1000 recs, 100/page?

Thanks!
0
Comment
Question by:APD_Toronto
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40629764
With MySQL, the easiest way is to use the LIMIT statement and adjust the first number for the start of the current and the second number is the number of rows.  http://dev.mysql.com/doc/refman/5.5/en/select.html
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40629784
You have a bit of an "interesting" problem with paging.  Most of the world is moving away from it and using a JavaScript front end like AngularJS instead.  For a pure "paging" solution, see this (admittedly several years old, but well intentioned and probably still useful):
http://www.sitepoint.com/perfect-php-pagination/

The idea Dave shares about using the LIMIT clause in the query makes sense to me.  The risk of not using a LIMIT clause is that the PDO results set may be too large to retrieve.  All of it comes back at once.  If you have wide rows (lots of columns) and numerous rows (maybe a query without a tight WHERE clause and a restrictive LIMIT) you can get a very large results set.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40630663
Assuming you pass in your page number as a query string:

     getRecord.php?page=1

Something like this should give you a good starting point:

$stmt->prepare("SELECT something FROM somewhere WHERE something=somethingElse LIMIT :limit, 100");
$stmt->bindparam(":limit", $limit, PDO::PARAM_INT);

$pageNum = (isset($_GET['page']) && is_numeric($_GET['page'])) ? $_GET['page'] : 1; 
$limit = ($pageNum - 1) * 100;

$stmt->execute();

Open in new window


You'll probably want to make your page checking logic a little more robust, but that's then general idea.
0
 

Author Comment

by:APD_Toronto
ID: 40631003
Exactly, I am using paging to avoid 1,000+ records being returned at once and slow down the application.

I was thinking using combination of LIMIT and I think OFFSET?
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40631033
That's exactly what my code shows you. Look at the prepared statement. You'll see it ends like this:

LIMIT :limit, 100

This means that you would pass in the OFFSET (starting row) as a named parameter called limit, because that would change for each page, and you return 100 records from that OFFSET.

If there's something in there you don't understand, then ask away...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question