How to catch database errors in CodeIgniter PHP

I am new to CodeIgniter, PHP and MySQL. I want to handle database errors. From one of the posts on Stackoverflow, I knew that following a statement could catch an error.

$this->db->_error_message(); 

But I can't figure out the exact syntax for using this. Suppose I want to update table entries with the name "table_name" with the following expression:

 $array['rank']="8"; $array['class']="XII"; $this->db->where('roll_no',$roll_no); $this->db->update("table_name", $array); 

Here, in the code above, I want to catch a DB error whenever a DB level violation occurs, i.e. either the field name is invalid or there is some unique violation of the constraint. If anyone helps me fix this, I will be very grateful. Thanks.

+6
source share
3 answers

codeIgniter has functions for it

 $this->db->_error_message(); $this->db->_error_number(); if(!$this->db->update("table_name", $array)) { $this->db->_error_message(); $this->db->_error_number(); } 
+6
source

You can debug a database error in the database configuration in (config / database.php) as follows:

 $db['default']['db_debug'] = TRUE; 

Read more here

You can also use Profiler to view all requests and their speed. In the controller you can put this:

 $this->output->enable_profiler(TRUE); 

Read more here

+5
source

In Codeigniter version 2,

 $this->db->_error_message(); $this->db->_error_number(); 

In Codeigniter version 3,

 $db_error = $this->db->error(); echo '<pre>';print_r($db_error);echo '</pre>'; 
+1
source

Source: https://habr.com/ru/post/950524/


All Articles