使用$this->;db->;代码点火器+PostgreSql中的num_rows()


Using $this->db->num_rows() in Codeigniter + PostgreSql

我通过PDO使用Codeigniter 3.0.4+PostgreSql。我只想得到我刚刚更新的行数。模型的相关剪切:

public function some_count_update($sid)
{
    $this->db->set('some_field', 'some_field+1', FALSE);
    $this->db->where('id', $sid, FALSE);
    $this->db->update('some_table');
    $this->db->affected_rows();
    return ($this->db->num_rows()>0) ? true : false;
}

PHP返回错误:

Fatal error: Call to undefined method CI_DB_postgre_driver::num_rows() in ...

我想问题是,CI使用php函数num_romws(),但对于Postgres,php有一个特殊的函数pg_num_rows()。我试图修改CI的系统类(DB_result>num_rows(),添加了前缀"pg_"),但无济于事。

要在更新中检查受影响的行,请使用$this->db->affected_rows()而不是$this->db->num_rows(),并在反勾中使用列名

public function some_count_update($sid)
{
    $this->db->set('some_field', '`some_field`+1', FALSE);// column name in backtick
    $this->db->where('id', $sid);// remove false 
    $this->db->update('some_table');
    return ($this->db->affected_rows()>0) ? true : false;// use affected_rows
}

读取https://www.codeigniter.com/user_guide/database/examples.html

这很好:)

return ($this->db->where('id', $sid)->get('some_table')->num_rows()>0) ? true : false;