Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the result of active record insert in CodeIgniter

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.

like image 654
Magic Avatar asked Dec 03 '22 03:12

Magic


1 Answers

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.

like image 104
danneth Avatar answered Jan 10 '23 04:01

danneth