Check codeigniter query errors instead of showing them to the user

☆樱花仙子☆ 提交于 2020-01-09 09:50:10

问题


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();
}  


来源:https://stackoverflow.com/questions/6600125/check-codeigniter-query-errors-instead-of-showing-them-to-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!