开发者

Check codeigniter query errors instead of showing them to the user

开发者 https://www.devze.com 2023-03-18 21:45 出处:网络
How can I check if a query went wrong instead of showing errors to users (containing database info which is unsafe).

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开发者_Python百科 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?


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


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


you could check it like:

function myFunction() {
  echo 'Oeps';
}

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

精彩评论

暂无评论...
验证码 换一张
取 消