[英]how to count the number of rows returned by query in Codeigniter with Datamapper
我在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);
在上面的代碼中,“$ u”是數據映射器“User”的類“User”的對象名,其中我的數據庫中的表名是“users”。 我想看看執行查詢后返回了多少行。 即使用戶ID和密碼不匹配,“$ total”也始終顯示1。 我想要的是,如果行數返回1則“ok”否則“出錯”。 我知道它的基本但如果有人知道那么請幫助我。 提前致謝。
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();
如果您只想計算找到的總行數,請調用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);
您可以查看文檔以獲取更多詳細信息。
如果密碼已加密,請重試
或者如果所有數據部分都很好,請嘗試使用以下代碼
$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.