简体   繁体   English

如何使用Datamapper计算Codeigniter中查询返回的行数

[英]how to count the number of rows returned by query in Codeigniter with Datamapper

I am using the following query in controller of codeigniter. 我在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". 在上面的代码中,“$ u”是数据映射器“User”的类“User”的对象名,其中我的数据库中的表名是“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. 即使用户ID和密码不匹配,“$ total”也始终显示1。 What I want is , if number of rows returned 1 then "ok" else "something wrong". 我想要的是,如果行数返回1则“ok”否则“出错”。 I know its basic but if somebody know it then please help me out. 我知道它的基本但如果有人知道那么请帮助我。 Thanks in advance. 提前致谢。

Following is availab in CI - checkout this page - http://codeigniter.com/user_guide/database/results.html CI中提供了以下内容 - 请访问此页面 - 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是查询结果对象分配给的变量:

$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 just want to count the total rows found, call the count() method instead of get() . 如果您只想计算找到的总行数,请调用count()方法而不是get()

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

But, if you also need the data, then you can simply call get() , and after that use PHP count() to count how many elements are there inside the all property of the object. 但是,如果你还需要数据,那么你可以简单地调用get() ,然后使用PHP count()来计算对象的all属性中有多少个元素。

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

You can check out the documentation for more details. 您可以查看文档以获取更多详细信息。

If the password was encrypted and try again 如果密码已加密,请重试

or if all the data part is fine the try to use the following code 或者如果所有数据部分都很好,请尝试使用以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM