• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Substitute for 'die ()' in PHP?

Cheap Bastard

New Member
PHP:
$dbconn = @mysql_connect($host,$dbuser,$dbpass) or die("Couldn't select database");
$db = @mysql_select_db($dbname,$dbconn) or die("Couldn't select database");
That's just a basic MySQL connect to database thingie. Works just fine, and does what it's supposed to.

However, the problem is that in the unhappy event that the MySQL database is down, the entire page stops being processed wherever the first error is. So what I'd prefer is instead of
PHP:
 or die("...");
something like "or goto some point way down the line where the MySQL connection's not needed anymore, and if you have to do this display some error message".

Thanks,
 
PHP:
$dbconn = @mysql_connect($host,$dbuser,$dbpass) or header("Location: http://www.domain.com/mysqlerror.php");
$db = @mysql_select_db($dbname,$dbconn) or header("Location: http://www.domain.com/myslqerror.php");
you'd need a preformatted error page though, but at least it'll fully load.
 
yea but... well that'd work just fine of course, but...
1) if i've given ANY output at all, it won't work
2) everytime the MySQL database is down, you get an error-page instaed of the real thing


what i'm really looking for is when a script hits a problem with the database (and only the database, all other errors should be programming errors), the script goes somewhere to the bottom and gives back an error.

Now you might ask, how could this be useful? Well, say i've got Index.php, and on it i have an include "counter.php"
well if the counter has a MySQL problem, the rest of the page won't load (unless you put the counter at the bottom but then you might as well make an invisible counter). There's also some stuff you can't include at the bottom due to placement problems...

The problem is, when counter.php hits a 'die()' function, index.php also dies, and the visitor gets shown the logo and the nav panel (wooptiedoo).

That will be useful for a few occasions though, so thanks anyway.
 
This is part of how to do it:

if(!$db = @mysql_connect("$dbhost", "$dbuser", "$dbpasswd"))
$error = "No DB Conn or whatever i want here for the visitors to see";
if(!@mysql_select_db("$dbname",$db))
$error = "No DB Found or whatever i want here for the visitors to see";


now my only problem is how to skip all the code that follows... I remember in batch files there was something like "goto point3" and it would skip everything until it found "point3: ..."

Is there something like that in PHP or would i have to get messy and entangle it in countless "if ... else" statements
 
Back
Top