Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter num_行和受影响的_行上下文_Php_Mysql_Codeigniter - Fatal编程技术网

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(),基于你的新代码,我已经更新了我的答案。我认为这将有助于你解决目前的问题。