Solved

PDO Paging

Posted on 2015-02-24
5
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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 count occurrences of each item in an array.

705 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