Getting a count of table or query rows in CodeIgniter 4 is quite easy using either of the Query Builder countAll() or countAllResults() functions. Continue reading to see examples of each…
Discover premium articles, in-depth guides, instructional videos, and much more by joining the “MySQL Learning Tier” membership. You have access to exclusive content unpublished anywhere else with this membership. With new content being added regularly, continue learning MySQL at any level.

Get tailored articles with Refind delivered each day in your inbox. Refind is part of my daily reading habit. Make it part of yours by using my referral link. At no additional cost to you, I will be eligible for a premium subscription with more sign-ups from my link. “The essence of the web, every morning in your inbox. Subscribe for free“
Self-Promotion:
If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!
CodeIgniter 4 Query Builder countAll()
The CodeIgniter 4 Query Builder countAll() function returns the count of rows in a table as an INTEGER. In this first example, we get the count of rows in the Sakila database ‘city’ table:
Number of rows in the city table is: 600
I have a story to tell. All about my journey as a self-taught backend web developer. Find out more in this interview I participated in over on the Real Tough Candy website. Read the entire interview here.
CodeIgniter 4 Query Builder countAllResults()
Where countAll() returns the number of rows in a specific table, countAllResults() returns the number of rows from a Query Builder query. You can use filtering functions with countAllResults() as well. In this example, I’m filtering the query using the where() function:
Number of rows in the actor table with actor_id less than or equal to 10 is: 10
Related: The CodeIgniter 4 Query Builder where() function supports several different parameter binding options. Learn about them in the post, CodeIgniter 4 Query Builder where() function and parameter structure with MySQL.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
Using the $db->getLastQuery() method, we can see the exact query that was sent to and executed on the server:
Related: Read the post CodeIgniter 4 Query Helper $db->getLastQuery() method for SQL prototyping for more information about this super-handy CodeIgniter 4 database helper method.
Are you a Medium member? If so, receive an email notification each time I publish a blog post if you prefer the Medium platform. Not a member? No worries! Use my sign-up link (I will get a commission at no extra cost to you) and join. I really enjoy reading all the great content there and I know you will too!!!
Support my blog and work by tossing any spare change you want in my Tip Jar. Thank you!
Similar reading
If you enjoyed this post then you may also like any of the following articles:
- Codeigniter 4 Last Insert ID
- Update data with CodeIgniter 4 update() function
- MySQL IFNULL() Function
- MySQL SHOW TABLES
Thank you for reading this post. Please share it with someone else who would enjoy it as well.
Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.
Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.
More ways I can help
- Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none.
- 🔒5 Truths I’ve Come To Realize As a Self-taught Developer
- Desktop and mobile wallpapers, digital downloads, photography services, Shopify and WooCommerce customizations, and content writing – all in one E-commerce Shop. Find your next digital purchase today!
- Take your Laravel applications next level with Battle Ready Laravel by Ash Allen. Learn how to improve the performance, maintainability, and security of your Laravel projects in this e-book.
Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.