PHP - Count Total Row In PDO
Submitted by razormist on Sunday, December 29, 2019 - 22:02.
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.
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!
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.
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.- <?php
- $conn=new PDO( 'mysql:host=localhost;dbname=db_count_pdo', 'root', '');
- if(!$conn){
- }
- ?>
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="cotaniner-fluid">
- <a class="navbar-brand">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Count Total Row In PDO</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <form method="POST" action="save.php">
- <div class="form-group">
- <label>Firstname</label>
- <input type="text" name="firstname" class="form-control" required="required"/>
- </div>
- <div class="form-group">
- <label>Lastname</label>
- <input type="text" name="lastname" class="form-control" required="required" />
- </div>
- <div class="form-group">
- <label>Age</label>
- <input type="number" name="age" class="form-control" min="0" max="200" required="required" />
- </div>
- <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
- </form>
- </div>
- <div class="col-md-8">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Age</th>
- </tr>
- </thead>
- <tbody>
- <?php
- require_once 'conn.php';
- $sql = "SELECT * FROM `member`";
- $query = $conn->prepare($sql);
- $query->execute();
- while($fetch = $query->fetch()){
- ?>
- <tr>
- <td><?php echo $fetch['firstname']?></td>
- <td><?php echo $fetch['lastname']?></td>
- <td><?php echo $fetch['age']?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- <br />
- <form method="POST">
- <button class="btn btn-success" name="count">Count Rows</button>
- </form>
- <br />
- <?php include'count.php'?>
- </div>
- </div>
- </body>
- </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.- <?php
- require_once 'conn.php';
- $firstname = $_POST['firstname'];
- $lastname = $_POST['lastname'];
- $age = $_POST['age'];
- try{
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "INSERT INTO `member`(firstname, lastname, age) VALUES ('$firstname', '$lastname', '$age')";
- }catch(PDOException $e){
- echo $e->getMessage();
- }
- $conn = null;
- }
- ?>
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.- <?php
- require_once 'conn.php';
- $sql = "SELECT COUNT(*) as total FROM `member`";
- $query = $conn->prepare($sql);
- $query->execute();
- $fetch = $query->fetch();
- echo "There are total of <span class='alert-danger'>".$fetch['total']."</span> rows";
- }
- ?>
Add new comment
- 869 views