I am using the following query in controller of codeigniter.
$u -> where('us_email_id', $username);
$u -> where('us_password', $password1);
$details = $u -> get();
$total = count($details);
echo $total; echo "<br>";
echo count($details);
In the above code "$u" is the object name for the class "User" for the datamapper "User" where the table name in my database is "users". I want to see how many rows are returned after executing the query. "$total" always displays 1 even if userid and password is not matched. What I want is , if number of rows returned 1 then "ok" else "something wrong". I know its basic but if somebody know it then please help me out. Thanks in advance.
Getting a count of table or query rows in CodeIgniter 4 is quite easy using either of the Query Builder countAll() or countAllResults() functions.
CodeIgniter - Count Number of Rows in Table: If you want to count just the number of rows in a database table you can use the function $this->db->count_all() . Just pass the table name to the method to get the row count like this, echo $this->db->count_all('Employees'); // Outputs, 8.
Following is availab in CI - checkout this page - http://codeigniter.com/user_guide/database/results.html
$query->num_rows()
The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to:
$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();
so that in your example above you need to try
$details->num_rows();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With