6

I am new to CodeIgniter, PHP and MySQL. I want to handle the DB generated errors. From one of the post in Stackoverflow, I knew that by following statement one can catch the error.

 $this->db->_error_message();

But I cannot figure out the exact syntax of using that. Suppose I want to update the records of table named "table_name" by the following statement:

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

Here in the above code I want to catch the DB error whenever any DB level violation occurs i.e. either field name is not valid or some unique constraint violation occurs. If anyone helps me to fix that I would be really grateful. Thank you.

1
  • Have you tried try..catch block? Catch(Exception $e) { echo $e->getMessage(); } Commented Jul 29, 2013 at 12:00

3 Answers 3

5

You can debug the database error on database configuration in (config/database.php) like this:

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

More info read here

Also you can use Profiler to see all the queries and their speed. In controller you can put this:

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

More information read here

Sign up to request clarification or add additional context in comments.

Comments

5

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(); 
}

2 Comments

I know these two functions. Could you please tell me where should I put it in the above code and how?
i don't get that how to apply or use this "$this->db->_error_message();" so can you help me over this ?? @RajeevRanjan
2

On Codeigniter version 2,

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

On Codeigniter version 3,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.