11

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?

3 Answers 3

32

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

@clami219 but should I display db error messages to user or should I customize the message for user? what is the best practice?
@MuhammadTarique I guess you wrote to the wrong user... I didn't write the answer, I just edited it (to add a semicolon). I guess you meant to address the author of the answer...
@MuhammadTarique It is generally best practice to log the error and then show the user a separate, custom message.
6

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

1 Comment

Yes using $errors_array['code']; and $errors_array['message']
-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();
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.