Welcome Guest, Not a member yet? Register   Sign In
How to catch database operation errors
#1

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
class Users_model extends CI_Model {
    public function deleteUser($userID)
    {
        
$this->db->where('id'$userID);
        
$result $this->db->delete('users');
        return 
$result;


Controller
PHP Code:
<?php
class Users extends CI_Controller {

    function 
__construct()
    {
        
parent::__construct();
        
$this->load->model('users_model');
    }

    public  function deleteUser()
    {
        
$userID $this->input->post('user_id');
        $data $this->users_model->deleteUser($userID);
        echo 
json_encode($data);
    }



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
            $('.btn-delete-user').on('click', function(){
                var user_id                "<" + $(this).data("id");
                $.ajax({
                    type "POST",
                    url  "<?php echo site_url('users/deleteUser')?>",
                    dataType "JSON",
                    data : {user_id:user_id},
                    success: function(data){
                        loadUsers();
                    },
                    error : function() {
                        alert("Some error happened");
                    }
                });
                return false;
            }); 

Kind regards
Alain
Reply


Messages In This Thread
How to catch database operation errors - by _Alain_ - 11-13-2019, 01:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB