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

时间:2022-05-31 09:11:55

This question already has an answer here:

这个问题在这里已有答案:

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

I have tried this, but getting this error

我试过这个,但是得到了这个错误

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 个解决方案

#1


0  

You should try this

你应该试试这个

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

#2


0  

You haven't performed the actual query yet. If you want to perform the query and then find out the number of results, use Yami's answer. If you want to know the number of results before getting the actual results, you can use this:

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

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

The latter is useful when you need to do processing which is based on the amount of rows the query returns, for example pagination.

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

#3


0  

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

#4


0  

If you are looking for number of rows returned by your query use,

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

query->num_rows()//This returns number of affected rows.

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

Make sure that, you execute this after $this->db->get();

确保在$ this-> db-> get()之后执行此操作;

So the sequence of the steps to be executed should be

所以要执行的步骤顺序应该是

$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.

Refer to CodeIgniter Manual for more.

有关更多信息,请参阅CodeIgniter手册。

#1


0  

You should try this

你应该试试这个

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

#2


0  

You haven't performed the actual query yet. If you want to perform the query and then find out the number of results, use Yami's answer. If you want to know the number of results before getting the actual results, you can use this:

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

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

The latter is useful when you need to do processing which is based on the amount of rows the query returns, for example pagination.

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

#3


0  

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

#4


0  

If you are looking for number of rows returned by your query use,

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

query->num_rows()//This returns number of affected rows.

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

Make sure that, you execute this after $this->db->get();

确保在$ this-> db-> get()之后执行此操作;

So the sequence of the steps to be executed should be

所以要执行的步骤顺序应该是

$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.

Refer to CodeIgniter Manual for more.

有关更多信息,请参阅CodeIgniter手册。