Solved

PDO Paging

Posted on 2015-02-24
5
174 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 82

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 42

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 42

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

762 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

18 Experts available now in Live!

Get 1:1 Help Now