Page 1 of 1

Die

Posted: Sun Mar 22, 2009 8:01 am
by Stela
Hello...

mysql_select_db('phpcake') or die('Cannot select database');

"or die" is really necessary or is just for precaution?

Thank you!

Re: Die

Posted: Sun Mar 22, 2009 10:09 am
by jayshields
It's a precaution, but it should be used or it will lead to problems in your script if you try to use the MySQL connection for anything after selecting the database failed.

Re: Die

Posted: Sun Mar 22, 2009 2:49 pm
by Stela
Thanks for the answer.

8)

Re: Die

Posted: Sun Mar 22, 2009 4:37 pm
by Bill H
I've always considered die() to be a rather ugly way to handle errors, because it does nothing other than terminate the script with a message.
It's okay for debugging, but for the release script I prefer a somewhat more sophisticated way of continuing the script on an alternate path.

Code: Select all

 
$Host = "xxx.xxx.xxx"; 
$User = "me";
$Password = "password";
$Link = @mysql_pconnect($Host, $User, $Password);
if ($Link)
{    $dbmName = "whatever";
      mysql_select_db($dbmName, $Link); 
     // etcetera
}
else
{
     echo "Failed to connect to database";
    // and take other measures as needed
}
 
That's a bit oversimplified, as the db selection can also fail, but you can get the idea.