Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PDO Paging

Posted on 2015-02-24
5
Medium Priority
?
220 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 84

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 111

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 44

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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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.
Suggested Courses

971 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