0
votes

So I'm rewriting our internal intranet to use the CodeIgniter framework. Up until now, I've been using $this->db->affected_rows() to determine whether rows were inserted, updated or deleted.

If this method returns TRUE, then I assume the SQL has succeeded. Unfortunately, this fails when no records were updated.

For example, I want to update all records in the database, if there aren't any records which match the where clause, even though the SQL has actually worked, affected_rows() will return FALSE because nothing was updated.

I've looked on Stackoverflow, and people are suggesting to use $this->db->_error_message() and the _error_code() equivalent. From what I've read, to use these functions, I need to turn off DB_DEBUG, which I don't want to do, because I'm still rewriting the intranet on a private server, and want to easily view the error message without trawling through log files.

Is there any way to keep db_debug set to true, and call a method which returns TRUE or FALSE based on whether the query was successful or not?

1

1 Answers

0
votes

You can turn off DB_DEBUG and still view the error messages (if any).

$config['db_debug'] = FALSE;

...

$this->db->query('UPDATE all.tables SET x=1 WHERE y=2');//update all tables; y is never 2

$ar = $this->db->affected_rows();

if($ar === 0){
    if($this->db->_error_message()){
        //there was a db error; print it
        echo $this->db->_error_message();//and error_code if you want
    }else{
        echo "No records affected, also no db error <br>";
        echo $this->db->last_query();//see last query if you want
    }
}else{
    echo "$ar rows affected";
}