Audio recording using PHP Codeigniter
How to Record Audio and Upload to Folder in Codeigniter Today we are going to explain how to record manually and upload it to a folder then insert that audio file into the Database using the Codeig
Read MoreSign Up Now and Get FREE CTO-level Consultation.
When developing a web application using PHP CodeIgniter, one of the most common database operations is counting the number of rows in a table. Whether you're displaying the total number of registered users, published blog posts, completed orders, or active products, row counting plays an essential role in building dashboards, reports, and analytics.
Fortunately, CodeIgniter provides simple and efficient methods to count records without writing complex SQL queries. Its Query Builder class makes counting rows secure, readable, and easy to maintain.
In this guide, you'll learn different ways to count rows in a database table using CodeIgniter, including counting all records, counting records with conditions, grouping data, and improving performance. Whether you're a beginner or an experienced developer, this tutorial will help you understand the best practices for counting rows efficiently.
Counting rows helps developers display meaningful information throughout an application. Some common use cases include:
Total registered users
Number of products
Active customers
Pending orders
Completed transactions
Blog post count
Employee records
Student database reports
Inventory management
Dashboard statistics
Instead of retrieving every record from the database, counting rows directly improves application performance.
Before getting started, ensure you have:
PHP installed
CodeIgniter project setup
MySQL database
Database configuration completed
A sample database table
Example table:
users
-------------------------------------
id
name
status
created_at
Suppose this table contains multiple user records.
One of the easiest ways to count records is by using countAll().
$this->db->count_all('users');
Output
250
This returns the total number of records available in the users table.
The function directly executes an SQL query similar to:
SELECT COUNT(*) FROM users;
It is simple, fast, and ideal when you need the total number of rows.
Another popular method is using Query Builder.
$this->db->from('users');
$count = $this->db->count_all_results();
Output
250
This method becomes more useful when conditions are applied before counting.
Often, developers only need specific records.
Example:
Count active users.
$this->db->where('status',1);
$this->db->from('users');
$count = $this->db->count_all_results();
Generated SQL
SELECT COUNT(*)
FROM users
WHERE status=1;
Output
180
This method is commonly used in admin dashboards.
Examples include:
Active users
Paid customers
Approved comments
Delivered orders
Published articles
Another approach is retrieving records first and then counting them.
Example
$query = $this->db->get('users');
$count = $query->num_rows();
Output
250
Although this works, it is not recommended for large datasets because all records are fetched before counting them.
| count_all() | num_rows() |
| Counts directly in database | Counts after fetching records |
| Faster | Slower for large tables |
| Uses COUNT(*) | Retrieves complete result |
| Better performance | Higher memory usage |
For large projects, always prefer count_all() or count_all_results().
Sometimes multiple filters are required.
Example
$this->db->where('status',1);
$this->db->where('country','India');
$this->db->from('users');
$count = $this->db->count_all_results();
SQL
SELECT COUNT(*)
FROM users
WHERE status=1
AND country='India';
This is useful for reports based on multiple criteria.
Suppose you want to count users whose names start with "A".
$this->db->like('name','A','after');
$this->db->from('users');
$count = $this->db->count_all_results();
Equivalent SQL
SELECT COUNT(*)
FROM users
WHERE name LIKE 'A%';
Useful examples include:
Product search
Customer search
Employee lookup
Blog title filtering
Many applications display reports based on date ranges.
Example
$this->db->where('created_at >=','2026-01-01');
$this->db->where('created_at <=','2026-12-31');
$this->db->from('users');
$count = $this->db->count_all_results();
SQL
SELECT COUNT(*)
FROM users
WHERE created_at
BETWEEN '2026-01-01'
AND '2026-12-31';
Useful for:
Monthly registrations
Daily reports
Annual statistics
Sales reports
Keeping database logic inside models makes your application cleaner and easier to maintain.
class UserModel extends CI_Model
{
public function totalUsers()
{
return $this->db->count_all('users');
}
}
$data['users'] = $this->UserModel->totalUsers();
$this->load->view('dashboard',$data);
Total Users :
<?php echo $users; ?>
This follows the MVC architecture recommended in CodeIgniter.
Counting rows is one of the most common database operations in PHP CodeIgniter, and the framework offers several efficient methods to accomplish it. Whether you need the total number of records, filtered counts, grouped data, or distinct values, CodeIgniter's Query Builder provides a clean and secure solution.
For the best performance, use count_all() for total records and count_all_results() for filtered queries. Avoid fetching unnecessary data with num_rows() when working with large tables, and follow the MVC architecture by placing database logic inside models.
By applying the techniques and best practices covered in this guide, you can build faster, more scalable, and easier-to-maintain CodeIgniter applications that deliver accurate statistics and reporting.
Use the count_all() method.
$this->db->count_all('users');
It returns the total number of records in the specified table.
count_all() counts every row in a table, while count_all_results() counts rows after applying filters such as WHERE, LIKE, or JOIN.
Yes. Apply the condition before calling count_all_results().
$this->db->where('status', 1);
$this->db->from('users');
$count = $this->db->count_all_results();
It works, but it retrieves all matching rows before counting them. For large tables, count_all() or count_all_results() is more efficient.
Yes. You can use JOIN along with COUNT() to count related records across multiple tables.
Use SQL's COUNT(DISTINCT column_name) with CodeIgniter's Query Builder.
Yes. Use GROUP BY along with COUNT() to display counts for each category.
count_all() is the fastest for counting all records, while count_all_results() is best for filtered queries.
Absolutely. Counting rows is commonly used for displaying users, products, orders, reports, and other key business statistics.
Query Builder makes your code easier to read, simplifies query construction, improves maintainability, and helps protect against SQL injection.
Request a FREE Business Plan.
+91 ▼ How to Record Audio and Upload to Folder in Codeigniter Today we are going to explain how to record manually and upload it to a folder then insert that audio file into the Database using the Codeig
Read More
Are you a skilled person to design and develop an impressive and user-friendly website? if not, Please wait don’t take a risk and don’t waste your time and energy on doing things. It is ad
Read More
Any company that wants to succeed in the fast-paced digital world of today needs to have a well-designed website. Your company may stand out from the competitors by having a good website, especially i
Read More