Welcome Guest, Not a member yet? Register   Sign In
Forcing CodeIgniter to show 404 page instead of any other error like 1064
#1

[eluser]Unknown[/eluser]
How can replace error pages like

Code:
A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL     server version for the right syntax to use near 'order by rand()' at line 1
with my website's default 404 error page?
#2

[eluser]Aken[/eluser]
You should never have query errors like this on your production website, nor should you ever hide them like nothing happened. Fix the problem, don't suppress it.
#3

[eluser]Otemu[/eluser]
Hi,

What Aken said above is an essential thing to do, as I kind of misread the question. Fix your errors!!!!!

If however a user requests a page and your database returns no results then you would need to run a check on the query to test if it returns any results, for example you could have code like this in your model
($query->num_rows() > 0) {
// query successful return true
} else {
// query failed return false
}
Then in your controller have it check if this is true or false, if false then run 404 function. Good example here


#4

[eluser]Unknown[/eluser]
Thanks a lot Aken & Otemu. I am fixing it .
#5

[eluser]boltsabre[/eluser]
Quote:Then in your controller have it check if this is true or false, if false then run 404 function.

I personally like the handle the 404 direct in my model, saves running more if statements in the controller, keeping them leaner. Obviously this is just a personal preference and not suited to everyone nor every situation. In some situations you certainly want to return false to your controller and display/do something rather than show a 404 :-)

I have built my own helper file with a function called page_not_found($var=null), which basically just throws a 404 http header, redirects to my 404 page, and exits. I'm at work and don't have my code base here, but it's something like this (p.s. I've autoloaded the helper)
Code:
function page_not_found($var = null){
   //fire your php http 404 not found header

   //$var is a string that can be passed the to view so that you can
   //display some custom text on the 404 page to make it a bit more
   //user friendly. I like to put a little note (deleted, not found, etc) and
   // a link to the most relevant page
   $view = $this->load-view('errors/404_not_found', $var, true);
   echo $view;
   exit();
}

And then in my model, instead of returning false, I just call this:
Code:
($query->num_rows() > 0) {
  // query successful return true
} else {
  page_not_found('This post either does not exist or has been deleted by the owner.<br/>To go back to the main forum page <a >Click here</a>');
}

//fill out your <a> tag or use the CI anchor function, your choice.




Theme © iAndrew 2016 - Forum software by © MyBB