CodeIgniter Forums
How to return database errors ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to return database errors ? (/thread-23331.html)



How to return database errors ? - El Forum - 10-07-2009

[eluser]bhenbe[/eluser]
Hello all,

this is my first post here and sorry for my english (i'm belgian).

Here's my problem : i want to show the returned error from a database transaction. I use html parser.

My code :

Model :
Code:
function service_add_user($service_id, $user_id) {

$this->db->trans_start();
        
$this->db->query('INSERT INTO tbluserbyservice(userbyservice_serviceid, userbyservice_userid, userbyservice_role)
          VALUES (?, ?, ?)',
         array($service_id, $user_id, 1));
        
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE)
    return false;
            
return true;
        
}

Controller :
Code:
function service_add_user($service_id) {
    
$this->load->library('form_validation');

if ($this->form_validation->run('service_user_add') == FALSE)
    return array('message_type' => 'error-msg',
                 'message_title' => 'Ajout d\'utilisateur à un service',
                 'message_content' => validation_errors());
        
if ($this->service_model->service_add_user($service_id, $_POST['user_id']) == FALSE)
    return array('message_type' => 'error-msg',
         'message_title' => 'Ajout d\'utilisateur à un service',
         'message_content' => [--Dunno what i must use here--]);

return array('message_type' => 'valid-msg',
         'message_title' => 'Ajout d\'utilisateur à un service',
         'message_content' => 'L\'utilisateur a été ajouté.');
    
}

View :
Code:
{messages}
<div class="{message_type}"><p>{message_title}</p>{message_content}</div>
{/messages}

Thank you for your help !


How to return database errors ? - El Forum - 10-07-2009

[eluser]rogierb[/eluser]
try: $this->db->_error_message()

En welkom :-)


How to return database errors ? - El Forum - 10-07-2009

[eluser]bhenbe[/eluser]
Thank you ;-)

I tried this function but it does'nt work.

Here's the response with db debug on :

Quote:Error Number: 1054

Unknown column 'userbyservicez_serviceid' in 'field list'

INSERT INTO tbluserbyservice(userbyservicez_serviceid, userbyservice_userid, userbyservice_role) VALUES ('1', '2', 1)

(i used a syntax error to test...)

When i set debug off, i have no response with

Code:
function service_add_user($service_id) {
    
   $this->load->library('form_validation');

   if ($this->form_validation->run('service_user_add') == FALSE)
      return array('message_type' => 'error-msg',
         'message_title' => 'Ajout d\'utilisateur à un service',
         'message_content' => validation_errors());
        
   if ($this->service_model->service_add_user($service_id, $_POST['user_id']) === FALSE)
      return array('message_type' => 'error-msg',
         'message_title' => 'Ajout d\'utilisateur à un service',
         'message_content' => $this->db->_error_message());

   return array('message_type' => 'valid-msg',
      'message_title' => 'Ajout d\'utilisateur à un service',
      'message_content' => 'L\'utilisateur a été ajouté');
    
}



How to return database errors ? - El Forum - 10-07-2009

[eluser]InsiteFX[/eluser]
It's in the CodeIgniter user guide!

CodeIgniter user guide

Enjoy
InsiteFX


How to return database errors ? - El Forum - 10-07-2009

[eluser]bhenbe[/eluser]
I'm sorry but i don't find any function example wich return a sql error.

I already read the user guide.

I thought i can find an answer in the Managing Errors chapter :

Quote:if ($this->db->trans_status() === FALSE)
{
// generate an error (how ?)... or use the log_message() function to log your error (how ?)
}

Perhaps, i need to translate this page to understand...

CI manages sql error without debug mode or not ?

Thank you.


How to return database errors ? - El Forum - 10-07-2009

[eluser]rogierb[/eluser]
You have to catch the error in your model after the query. There will be no error after a succesfull transaction rollback.

As for custom error handling being in the userguide, I'm still looking... The only thing I've found is that you can controle the message, not the message itself. Can be blind though... Tongue


How to return database errors ? - El Forum - 10-07-2009

[eluser]bhenbe[/eluser]
Quote:You have to catch the error in your model after the query. There will be no error after a succesfull transaction rollback.

Thanks !