Count Rows in the Database Table Using PHP 7.2 

Count Rows in the Database Table Using PHP 7.2. In this article, we just wanted to talk about how you can count how many rows are there in the database. Suppose, if you wanted to find out how many users logged in to your website. How can you find that? if you are using the database to store user information like user password username or any kind of user data than this tutorial is for you. In this tutorial, you going to learn how to know how many users sign up to your website. Then this is the very simple example to just find out the rows of your database table.

In that situation, this tutorial will rescue you. We will just find out how many rows are there in the database table so we specify the unique ID for each user when he or she signs up to your website. You probably have seen that every time when you create the database table in my SQL or any kind of databases It’s really a good practice for the programmer to identify each row uniquely.  Like, to identify each row we use the primary key in MySQL. The same Idea we are using in this tutorial.

Now let’s take a look at the example and perform this example in your integrated development environment (IDE).



Now just create a  simple file sample.php. In this file, we are creating a class that gives us the number of rows in the database table. In this class, we create a simple connection which we created in the last article. clicking on this link you can read the previous article. We are not describing the connection again because we describe it in the previous article so if you just wanted to know about connection using object-oriented programming than a must read this article.

So the next thing we need to do it we create a function that returns the number of rows you had in the database table. To perform this type of program the one thing you always keep in mind that your connection property is always valid. The invalid connection property will induce an error.

Your sample file should look like this.

Code description

Now I just wanted to describe what we had done here. We just create a class with the name count. You are free to specify your own name for this class. Afterward, we just specify some connection properties. If you have a different username password or database then you can change this values also. Then we create a constructor and initialize the connection property with the MySql class. To know more about connection properties and the constructor function read the previous article.

Now you just need to call this get_data() function to get the number of records in your database table. We also move forward and give you and complete understanding on how you can store the database records in the array and access that record individually.

Now you just look closely to this get_data() function. Recall the SQL Query that returns the number of records. Using SQL select statement we just fetch all the database table records and store it in the result variable. Then using connection property we call query method to execute the select query. So using this method we execute this select query and store the records in the result variable.

Then we just create a simple $send the variable and initialize this 0. Using if statement we check the number of records in the database table.  if we have more than zero records in the database table then this condition will be true.  so we have more than one record in my table. So I will just move forward and fetch that data using while loop.

Using fetch associative method we fetch all the records at a time and increase the value of $send variable using the shorthand + +  operator and the last I will just return this send the variable to the function using return keyword.


Calling the Method

Now the last thing which you really need to know is how to call the method In the count class. To call this get_data() method we have an object for rescue. creating an instance of the class you can access all the properties and methods in the class if it is public or protected. We also created an article about public-private and protected variables in detail. So if you wanted to learn where you can use these three keywords then it will be a great thing for you.

Now just use this above code in your PHP program. In the above code, we just specify the number of database table rows in the Number variable. Using Eco statement we will print the numbers variable value. This number variable value is equal to the number of rows in your database table. That’s it now you know how you can get the database table rows in your PHP variable. Now you know how many users you have on your website. Now you know that how to count rows in the database table.

That is all for this article. The more article you read more you learn so keep reading. Thank you so much for reading this article we will take you back again in the next article.