Home >> FAQs/Tutorials >> MySQL Tutorials

MySQL Tutorial - Breaking Query Output into Pages

By: FYIcenter.com

(Continued from previous topic...)

How To Break Query Output into Pages?

If you have a query that returns hundreds of rows, and you don't want to present all of them to your users on a single page. You can break output into multiple pages, and only present 10 rows per page like what Google Website is doing. To do this, you need to modify your query with the LIMIT clause to return rows starting from the correct row number, and limited to 10 rows in the output.

The tutorial exercise below shows you how to break output into pages with 2 rows per page. It also calcualtes the total number of pages with a simple query criteria and order by condition. As an example, rows for page number 2 are returned.

<?php
  include "mysql_connection.php";
 
  $rowsPerPage = 2;
  $where = " WHERE url LIKE '%co%'";
  $order = " ORDER BY time DESC";
  
  $curPage = 2; 
  $start = ($curPage-1) * $rowsPerPage; 

  $sql = "SELECT COUNT(*) AS count FROM fyi_links" . $where . $order;
  print("SQL = $sql\n");
  $rs = mysql_query($sql, $con);
  $row = mysql_fetch_assoc($rs);
  $numberOfPages = $row['count'] / $rowsPerPage; 
  print("Number of pages = $numberOfPages\n");
  mysql_free_result($rs);
  
  $sql = "SELECT * FROM fyi_links" . $where . $order
    . " LIMIT ".$start.", ".$rowsPerPage;
  print("SQL = $sql\n");
  $rs = mysql_query($sql, $con);
  while ($row = mysql_fetch_assoc($rs)) {
    print($row['id'].", ".$row['url'].", "
    . $row['notes'].", ".$row['time']."\n");
  }
  mysql_free_result($rs);

  mysql_close($con); 
?>

If you run this script, you will get something like this:

SQL = SELECT COUNT(*) AS count FROM fyi_links 
   WHERE url LIKE '%co%' ORDER BY time DESC
Number of pages = 3

SQL = SELECT * FROM fyi_links WHERE url LIKE '%co%' 
   ORDER BY time DESC LIMIT 2, 2
101, dev.fyicenter.com, , 2006-07-01 20:24:46
102, dba.fyicenter.com, Nice site., 2006-07-01 20:24:46

(Continued on next topic...)

  1. How To Create a New Table?
  2. How To Get the Number of Rows Selected or Affected by a SQL Statement?
  3. How To Insert Data into an Existing Table?
  4. How To Fix the INSERT Command Denied Error?
  5. How To Insert Multiple Rows with a SELECT Statement?
  6. What Is a Result Set Object?
  7. How To Query Tables and Loop through the Returning Rows?
  8. How To Break Query Output into Pages?
  9. How To Update Existing Rows in a Table?
  10. How To Delete Existing Rows in a Table?
  11. How To Quote Text Values in SQL Statements?
  12. How To Quote Date and Time Values in SQL Statements?
  13. How To Display a Past Time in Days, Hours and Minutes?
  14. How To Perform Key Word Search in Tables?
  15. How To Build WHERE Criteria with Web Form Search Fields?
  16. How To Query Multiple Tables Jointly?
  17. How To Define the ID Column as Auto-Incremented?
  18. How To Get the Last ID Assigned by MySQL?

MySQL Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...