Php CodeIgniter num_行和受影响的_行上下文
这个问题可能有点愚蠢,但我不明白: 我的模型中有这两个函数Php CodeIgniter num_行和受影响的_行上下文,php,mysql,codeigniter,Php,Mysql,Codeigniter,这个问题可能有点愚蠢,但我不明白: 我的模型中有这两个函数 public function count() { return $this->db->num_rows(); } public function changes() { return $this->db->affected_rows(); } 当我调用changes()时在我的控制器中,它显示上次(更新)查询的受影响行。当我使用count()时但是,对于显示上次(选择)查询的行,我得到一个错
public function count()
{
return $this->db->num_rows();
}
public function changes()
{
return $this->db->affected_rows();
}
当我调用changes()时代码>在我的控制器中,它显示上次(更新)查询的受影响行。当我使用count()时代码>但是,对于显示上次(选择)查询的行,我得到一个错误
控制器中的代码如下所示:
if (!$this->synchronization_model->get_contact_knowledge($contact['account_id'], SERVER_LOCATION_ID)) {
throw new Exception("Failed to update knowledge");
}
if( $this->synchronization_model->count() == 0) {
$this->synchronization_model->insert_knowledge($contact['account_id'], $contact_server_time);
}
有没有办法解决这个问题
$this->db->num_rows();
如果在选择时返回结果已获取的n行数,则需要将其用作
$query = $this->db->get('table');
OR
$query = $this->db->query('select * from table');
$query->num_rows();
返回$this->db->受影响的行()代码>
更新、删除或插入时返回的行是否受到影响
在CodeIgniter中,num_rows()
函数是查询对象的方法,而不是数据库对象。。。看
例如,您可以按以下方式使用num_rows()
:
$query = $this->db->query("SELECT * FROM some_table WHERE some_col = 'blah'");
if($query->num_rows()){
//yay, data!
} else {
//no data for you
}
num_rows()
不是db
类的方法。应该针对resultset
对象调用它
$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();
正如您在这里看到的,我们不是调用$this->db->num_rows()
,而是调用$query->num_rows()代码>
作为解决方法,您可以将查询对象传递给count()
方法,如下所示:
public function count($query)
{
return $query->num_rows();
}
更新:
根据您更新的代码,我建议如下:
我假设查询是在同步\u model
中执行的。在这种情况下,你应该做的是。在synchronization\u model
中设置一个变量,比如行数
。并将num_rows()
return的值放入该变量。count()
中的函数仅返回此变量。因此,它将类似于:
内部同步\u模型
:
......
$query = $this->db->query('SELECT * FROM my_table');
$this->row_count = $query->num_rows();
......
public function count()
{
return $this->row_count;
}
......
下面是我如何正确修复的
代替此功能:
public function check_account_already_exists($email_address)
{
if ($query = $this->db->query('SELECT * FROM account WHERE email_address = ?', array($email_address)))
{
return true;
}
}
public function check_account_already_exists($email_address)
{
if ($query = $this->db->query('SELECT * FROM account WHERE email_address = ?', array($email_address)))
{
return $query;
}
}
我有这个功能:
public function check_account_already_exists($email_address)
{
if ($query = $this->db->query('SELECT * FROM account WHERE email_address = ?', array($email_address)))
{
return true;
}
}
public function check_account_already_exists($email_address)
{
if ($query = $this->db->query('SELECT * FROM account WHERE email_address = ?', array($email_address)))
{
return $query;
}
}
因此,当我在控制器中调用它时:
// Check whether the e-mail address has not already been taken
if (!($count = $this->account_model->check_account_already_exists($email_address))) {
throw new Exception("Failed to fetch account");
}
$this->account_model->count($count);
我可以那样使用它。。这样,上面的if语句仍将以相同的方式工作。但是,如果我想要/需要在类中使用不同的函数来选择和计数,该怎么办?在这种情况下,有效地传递并没有任何意义,真的。如果我是你,我不会为它编写单独的count()
函数。因为函数的生存/使用仅在查询执行后立即进行。因此,我会在查询执行后直接调用$query->num_rows()
,然后处理它。如果我这样做,并且没有行,我的第一段代码将失败(见上文),因为num_rows将返回0,这等于false。看起来很好,但找到了另一个解决方案。也许更好,因为我不需要重写很多代码。谢谢执行的查询在哪里,其中$store->count()
正在尝试对行进行计数?对不起,这是一段旧代码。。它的意思是读$this->synchronization\u model->count(),基于你的新代码,我已经更新了我的答案。我认为这将有助于你解决目前的问题。