Welcome Guest, Not a member yet? Register   Sign In
how to catch database errors in model
#1

(This post was last modified: 06-15-2015, 09:58 PM by mrajesh369.)

i am using a method  '$this->db->_error_message()'
i am writing the below code in my model

public function deal_citys($deal_id)
{
   $query=$this->db->query("select deal_location from deal where deal_id=$deal_id")->result();
   if ($query) {
   
  return $query[0]->deal_location;
}
else
{
     $this->db->_error_message();
   
}
}

i am not passing to deal_id in my controller.
how can i catch the db errors in codeigniter
please give me a examples
Reply
#2

If you have db_debug set in your database config, you won't really get a chance to catch the errors.

$this->db->_error_message() only works in CI2. CI3 does include an error() method in the db class to get the most recent error, if any have occurred:
http://www.codeigniter.com/user_guide/da...ing-errors

So, you would do something like this:

PHP Code:
public function deal_citys($deal_id)
{
 
   $query $this->db->query("select deal_location from deal where deal_id=$deal_id");
 
   if ($query->num_rows() > 0) {
 
       return $query->row()->deal_location;
 
   }

    
// It may be that $deal_id wasn't found, 
    // but we can check for an error, anyway.
 
   $error $this->db->error();

 
   // If an error occurred, $error will now have 'code' and 'message' keys...
 
   if (isset($error['message'])) {
 
       return $error['message'];
 
   }

    
// No error returned by the DB driver... 
    
return null;

Reply




Theme © iAndrew 2016 - Forum software by © MyBB