Solved

PDO Paging

Posted on 2015-02-24
5
176 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 108

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now