Results 1 to 3 of 3

Thread: Paging with PDO

  1. #1
    Join Date
    Apr 2006

    Paging with PDO


    I have an MySQL query which is seems to work fine with a PDO execute and I want to make paging to the results. I want to show the total number of results also. If I make paging by adding "limit :rownumber, :rowperpage" to the end of the query, I have to execute another query to get the total count number. If I don't, I don't know how to make paging. Any ideas?

    $searchquery = "Select propertycode,title,price,currency,altbolgeadi,bolgeadi,description,bedrooms,bathrooms from urunler where bolgeID=:location and altbolgeID=:sublocation and price between :pricemin and :pricemax limit :sayfa, :gosterim";
    $stmt = $db->prepare($searchquery);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $rowcount = count($rows);
    In above code, if I keep the limit section $rowcount becomes the limit given to MySQL. If I don't, I don't know how to make paging with the $rows results.

    Many thanks

  2. #2
    Join Date
    Aug 2004
    Since you're doing a fetchAll(), you could just do an array_slice() on the resulting array to get the desired page data, e.g.:
    PHP Code:
    $pageData array_slice($rows, ($pageNbr 1) * $numPerPage$numPerPage); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Apr 2006
    Hi, thanks for your reply. I checked array_slice on PHP manual understood what you mean.

    Many thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center