How to catch database operation errors |
Hi
I try to catch an error if something with the database operations goes wrong. I tried many variants of the following code but $data always returns true (it doesn't matter if the database operation produces an error or not). What do I have to do to catch an error if database operation went wrong? Model PHP Code: <?php Controller PHP Code: <?php View (see the "<" for var user_id. This should generate an error because the user with such an id cannot be found) PHP Code: // Delete user button Kind regards Alain
You will need to check in your PHP code, in case it's true or false.
If it's false you will need to send a different status header. https://codeigniter.com/user_guide/libra...tus_header jQuery will give you success on all 200 requests. And that's the default status header.
@jreklund is right,
i must add that API CONTROLLER has to handle the error processing, also please read on and implement correct REST API responses and http response codes, and JQUERY knows how to handle them what i would do is to check the result of the DB queries in the CONTROLLER and then return corresponding http headers and the payload (null, false, or nothting {}). then JQUERY can detect and handle the appropriate action.
I think I'm strugling over some basic missunderstanding.
The following, in my case, always returns true. Even it was not able to delete it. But why? From my point of view it should only return true if the record has been deleted. PHP Code: $result = $this->db->delete('users');
Ok I did now this:
PHP Code: $result = $this->db->where('id', $userID); Instead of this: PHP Code: $result = $this->db->delete('users'); The first one returns the following. But I can't find any error there (I compared it with a correct query and with a query that should fail, but both return the same): Code: dbdriver: "mysqli", compress: false, delete_hack: true, stricton: false, dsn: "", username: "root",…} The second one always returns true.
So what type of things are it failing on? It should only return FALSE if it couldn't delete a existing entry.
PHP Code: $error = $this->db->error(); // Has keys 'code' and 'message' What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
I extended the model as followed for testing purpose:
PHP Code: $this->db->where('id', $userID); But it always returns no error. Even if I put error() after db-delete. The response in Developer Tools is: Code: {"code":0,"message":""} |
Welcome Guest, Not a member yet? Register Sign In |