Check codeigniter query errors instead of showing

2019-01-18 05:59发布

问题:

How can I check if a query went wrong instead of showing errors to users (containing database info which is unsafe).

Let's say I have such situation where my paging class takes a URI segment to show page example.com/page/uri_segment. If someone write it like this example.com/page/bla_bla_bla I get an error that shows information about my database.

How can I handle this?

回答1:

In application/config/database.php set

// suppress error output to the screen
$db['default']['db_debug'] = FALSE;

In your model or controller:

// try the select.
$dbRet = $this->db->select($table, $dataArray);
// select has had some problem.
if( !$dbRet )
{
   $errNo   = $this->db->_error_number()
   $errMess = $this->db->_error_message();
   // Do something with the error message or just show_404();
}


回答2:

You can try this $this->db->error(); , this will get you the ErrorCode & Errormessage in array format.



回答3:

you could check it like:

function myFunction() {
  echo 'Oeps';
}

$res = mysql_query($sql);
if($res && mysql_num_rows($res)>0){
  echo 'Success';
} else {
   myFunction();
}