Counting the number of returned results with num_rows()
It's useful to count the number of results returned—often bugs can arise if a section of the code that expects to have at least one row is passed with zero rows. Without handling the eventuality of a zero result, an application may become unpredictably unstable and may give away hints to a malicious user about the architecture of the app. Ensuring correct handling of zero results is what we're going to focus on here.
How to do it...
- We're going to create a block of code for a model and controller. You may already have code in a controller, model, or view that does all or some of the following—obviously, you can skip any step that you do not need. Add or adapt the ensuing code into your controller: ...
Get CodeIgniter 2 Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.