[英]Check codeigniter query errors instead of showing them to the user
如何检查查询是否出错而不是向用户显示错误(包含不安全的数据库信息)。
假设我有这样的情况,我的分页 class 需要一个 URI 段来显示页面example.com/page/uri_segment
。 如果有人这样写example.com/page/bla_bla_bla
我会收到一个错误,显示有关我的数据库的信息。
我该如何处理?
在 application/config/database.php 中设置
// suppress error output to the screen
$db['default']['db_debug'] = FALSE;
在您的 model 或 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();
}
你可以试试这个$this->db->error();
,这将为您提供数组格式的错误代码和错误消息。
你可以像这样检查它:
function myFunction() {
echo 'Oeps';
}
$res = mysql_query($sql);
if($res && mysql_num_rows($res)>0){
echo 'Success';
} else {
myFunction();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.