Count Total Rows in Table Using PHP/MySQL

February 13, 2021
PHP
Count Total Rows in Table Using PHP Source Code

Tutorial: Count Total Rows in Table Using PHP and MySQL with Source Code

About How to Count Total Rows in Table Using PHP/MySQL

In this tutorial, we will create a Count Total Rows in Table using PHP MySQL. This code will display the total rows in the table. The code use MySQLi SELECT query to display the data from the database, then using mysqli_num_rows(), a PHP MySQL built-in function that will display the total rows of a table when the query is set. This a user-friendly program. Feel free to modify and use it for your system.

We will be using PHP as a scripting language that interprets in the web server, such as xamp, wamp, etc. It is widely used by modern website application to handle and protect user confidential information.

Getting Started to Count Total Rows in Table Using PHP

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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

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

count total rows in table using php mysql

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=mysqli_connect("localhost", "root", "", "db_rows");
 
    if(!$conn){
        die("Error: Faile to connect to database!");
    }
?>

Creating The Interface to Count Total Rows in Table Using PHP

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="containr-fluid">
            <a class="navbar-brand" href="https://campcodes.com">CampCodes</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Count Total Rows Source Code</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST" action="add_member.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>Address</label>
                    <input type="text" name="address" class="form-control" required="required"/>
                </div>
                <center><button class="btn btn-primary" name="add">Add Member</button></center>
            </form>
        </div>
        <div class="col-md-8">
            <?php include"count_rows.php";?>
            <br />
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        require_once 'conn.php';
                        $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
                        while($fetch = mysqli_fetch_array($query)){
                    ?>
                        <tr>
                            <td><?php echo $fetch['firstname']?></td>
                            <td><?php echo $fetch['lastname']?></td>
                            <td><?php echo $fetch['address']?></td>
                        </tr>
                    <?php
                        }
                    ?>
 
                </tbody>
            </table>		
        </div>
    </div>
</body>	
</html>

Creating the PHP Query

This code contains the php query of the application. This code will store the form inputs to the database server. To make this just copy and write these block of codes below inside the text editor, then save it as add_member.php.

<?php
    require_once 'conn.php';
 
    if(ISSET($_POST['add'])){
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $address = $_POST['address'];
 
        mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
 
        header("location: index.php");
    }
?>

Creating the Main Function

This code contains the main function of the application. This code will display the total rows of the database table. To make this just copy and write these block of codes below inside the text editor, then save it as count_rows.php.

<div class="form-inline">
    <label>Num of rows: </label>
    <?php
        require_once'conn.php';
        $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
        $rows=mysqli_num_rows($query);
 
        echo '<input type="text" name="rows" class="form-control" size="4" value="'.$rows.'" disabled="disabled"/>';
    ?>
</div>
There you have it we successfully created Count Total Rows in Table 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!

Leave a Reply

Your email address will not be published. Required fields are marked *