如何获取记录集变量中查询返回的行数?

时间:2014-01-31 11:49:16

标签: php codeigniter

$a=$this->db->where('username', $user);
echo $a->num_rows();

我试过这个,但收到此错误

Fatal error: Call to undefined method CI_DB_mysql_driver::num_rows() in C:\xampp\htdocs\Testing\CI\application\models\mymodel.php on line 20

4 个答案:

答案 0 :(得分:0)

$this->db->from('YOUR_TABLE);
$this->db->where('username', $user);
$result = $this->db->get();
echo $result->num_rows();

答案 1 :(得分:0)

您尚未执行实际查询。如果要执行查询然后找出结果数,请使用Yami的答案。如果您想在获得实际结果之前知道结果数量,可以使用:

$this->db->where('username', $user);
echo $this->db->count_all_results('yourtable');

当您需要根据查询返回的行数进行处理时,后者非常有用,例如分页。

答案 2 :(得分:0)

你应该试试这个

$a=$this->db->where('username', $user)->get('table_name')->num_rows();
echo $a;

答案 3 :(得分:0)

如果您要查询查询使用返回的行数,

query-> num_rows()//返回受影响的行数。

确保在$ this-> db-> get();

之后执行此操作

所以要执行的步骤顺序应为

$this->db->select('select condition'); 

$this->db->where('where condition');

$this->db->from('table name');

$Query = $this->db->get();

$Query->num_rows();  //Returns number of rows returned by the query.

有关详情,请参阅CodeIgniter手册。