Codeigniter – how to get the result of active record insert in CodeIgniter

activerecordcodeigniter

I'm using active record in CodeIgniter.
And I can insert data into database successfully.
But how to get the result of insert sql when fail?
Now it return a html say about the sql error.
I don't want this html content.

EDIT

$data = array(
   'title' => 'My title' ,
   'name' => 'My Name' ,
   'date' => 'My date'
);

$this->db->insert('mytable', $data); 

Simple code. But when the 'name' column has unique property. And I'm inserting a duplicate value to it. It return html content of sql error.

I just want it to return the error code and not output the html content.

Best Answer

To not get the DB error message make sure that you have $db['default']['db_debug'] = FALSE in the file /application/config/database.php.

Then after you've preformed your (attempted) insert you can run:

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

If the result is 0, your insert failed and you can present an error message of your own choosing.

Related Topic