How to count the number of rows returned by a query in Codeigniter using Datamapper

I am using the following query in a codeigniter controller.

    $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 name of the object for the User class for the user datamapper, where the table name in my database is "users". I want to see how many rows are returned after the query is completed. "$ total" always displays 1, even if the user ID and password do not match. I want if the number of rows returns 1, then "ok" else "something is wrong." I know its basic, but if someone knows this, please help me. Thanks in advance.

+5
source share
3

, count() get().

$u->where('us_email_id', $username);
$u->where('us_password', $password1);
$total = $u->count();

, , get(), PHP count(), all .

$u->where('us_email_id', $username);
$u->where('us_password', $password1);
$u->get();
$total = count($u->all);

.

+6

CI - - http://codeigniter.com/user_guide/database/results.html

$query->num_rows()

, . . $query - , :

$query = $this->db->query('SELECT * FROM my_table');

echo $query->num_rows(); 

$details->num_rows();
+8

,

$this->db->where(array('us_email_id' => $username,'us_password' => $password1));
echo $this->db->count_all_results('users');
+1

All Articles