[英]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.