PHP - Count Total Row In PDO

In this tutorial we will create a Count Total Row In PDO using PHP. This code can count the total rows in database server when user click the button. The code use PDO SELECT query to display data and by adding COUNT(*) as a query selector in order to count all the total rows in the table. This is a free program feel free to modify it or use it to your application. We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_count_pdo, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=new PDO( 'mysql:host=localhost;dbname=db_count_pdo', 'root', '');
  3. if(!$conn){
  4. die("Error: Failed to connect to database!");
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="cotaniner-fluid">
  10. <a class="navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Count Total Row In PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form method="POST" action="save.php">
  19. <div class="form-group">
  20. <label>Firstname</label>
  21. <input type="text" name="firstname" class="form-control" required="required"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Lastname</label>
  25. <input type="text" name="lastname" class="form-control" required="required" />
  26. </div>
  27. <div class="form-group">
  28. <label>Age</label>
  29. <input type="number" name="age" class="form-control" min="0" max="200" required="required" />
  30. </div>
  31. <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
  32. </form>
  33. </div>
  34. <div class="col-md-8">
  35. <table class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr>
  38. <th>Firstname</th>
  39. <th>Lastname</th>
  40. <th>Age</th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. <?php
  45. require_once 'conn.php';
  46.  
  47. $sql = "SELECT * FROM `member`";
  48. $query = $conn->prepare($sql);
  49. $query->execute();
  50.  
  51. while($fetch = $query->fetch()){
  52. ?>
  53.  
  54. <tr>
  55. <td><?php echo $fetch['firstname']?></td>
  56. <td><?php echo $fetch['lastname']?></td>
  57. <td><?php echo $fetch['age']?></td>
  58. </tr>
  59.  
  60. <?php
  61. }
  62. ?>
  63. </tbody>
  64. </table>
  65. <br />
  66. <form method="POST">
  67. <button class="btn btn-success" name="count">Count Rows</button>
  68. </form>
  69. <br />
  70. <?php include'count.php'?>
  71. </div>
  72. </div>
  73. </body>
  74. </html>

Creating the PDO Query

This code contains the pdo query of the application. This code will store the form data to the database server when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as save.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5.  
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $age = $_POST['age'];
  9.  
  10.  
  11. try{
  12. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $sql = "INSERT INTO `member`(firstname, lastname, age) VALUES ('$firstname', '$lastname', '$age')";
  14. $conn->exec($sql);
  15. }catch(PDOException $e){
  16. echo $e->getMessage();
  17. }
  18.  
  19. $conn = null;
  20.  
  21. header("location: index.php");
  22.  
  23. }
  24. ?>

Creating the Main Function

This code contains the main function of the application. This code will count all the data in table when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as count.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4.  
  5. if(ISSET($_POST['count'])){
  6. $sql = "SELECT COUNT(*) as total FROM `member`";
  7. $query = $conn->prepare($sql);
  8. $query->execute();
  9. $fetch = $query->fetch();
  10.  
  11. echo "There are total of <span class='alert-danger'>".$fetch['total']."</span> rows";
  12. }
  13. ?>
There you have it we successfully created a Count Total Row In PDO using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment