Welcome Guest, Not a member yet? Register   Sign In
how to catch database errors in model
#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


Messages In This Thread
RE: how to catch database errors in model - by mwhitney - 06-11-2015, 11:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB