Reputation: 1349
Hey everyone!
I'm building a webapp in PHP that uses a MySql DB. What I want to do is for example when someone tries to access to a page, but the DB throws an error, to display a HTML (or redirect) page with the HTTP code 500. Something like the "fail whale" in Twitter.
Is that possible?
Upvotes: 1
Views: 1978
Reputation: 82078
If there is an error in mysql it will return FALSE instead of a resource. You can then do the following to test and conditionally redirect:
// This also works for mysql_connect( ... ); and all other <dbtype>_<command>
// functions, including MySQLi, Oracle, and PSQL extensions.
$cond = mysql_query( $resource, $query );
if( $cond === FALSE )
{
// You can replace this with any other error handling you'd like.
header( "Location: url/of/error/page" );
die();
}
Upvotes: 0
Reputation: 15616
You could use the die function in PHP to do this like so.
mysql_connect("details go here")or die(require("failed.php"));
That will attempt to connect to your database and if it fails will require another file, you could also create a function that redirects users and stick that inside the die() function and simply redirect them to another page, either way that's how you would send users away upon an error with the connection.
Upvotes: 4