php 检查 codeigniter 查询错误而不是将它们显示给用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6600125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:49:53  来源:igfitidea点击:

Check codeigniter query errors instead of showing them to the user

phpmysqlcodeigniter

提问by baranq

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_blaI get an error that shows information about my database.

假设我有这样的情况,我的分页类需要一个 URI 段来显示 page example.com/page/uri_segment。如果有人这样写,example.com/page/bla_bla_bla我会收到一个错误,显示有关我的数据库的信息。

How can I handle this?

我该如何处理?

回答by cwallenpoole

In application/config/database.php set

在 application/config/database.php 中设置

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

回答by sushil

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

你可以试试这个 $this->db->error();,这会给你数组格式的错误代码和错误消息。

回答by PvdL

you could check it like:

你可以像这样检查:

function myFunction() {
  echo 'Oeps';
}

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