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…
Do you need to learn MySQL? Just starting and confused about how to query a table and get the data you need? I am creating premium MySQL Beginners content for this specific reason. To help those who want to learn the basics of MySQL but don’t know where to start. Learn more about the premium blog posts as I develop and release them.
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“
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
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!
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() functionMySQL IFNULL() FunctionMySQL SHOW TABLES
Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!
A Call To Action!
Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.
Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.
Be sure and visit the “Best Of” page for a collection of my best blog posts.
Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.
Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.
How can I help you?
Are you thinking of starting up a blog? I use WordPress for my blog. Let’s both save money on the plans offered. Grab a Gmail HTML Email Signature template from my Etsy shop and make your emails pop and stand out. Need hosting for your next web application or WordPress site? I use and highly recommend Hostinger. They have great pricing and service.I enjoy reading Refind: The essence of the web, every morning in your inbox. Subscribe for free. Help me get a premium subscription by signing up yourself with my referral link.Grab a free pack of mobile Creator wallpapers.Just getting started or wanting to learn MySQL? Find out about my premium blog posts and MySQL Beginner Series here.
The Newsletter for PHP and MySQL Developers
Disclosure: Some of the services and products links in this post 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 post Counting with countAll() and countAllResults() in CodeIgniter 4 appeared first on Digital Owl’s Prose.