Die

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Die

Post by Stela »

Hello...

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

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

Thank you!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Die

Post 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.
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Re: Die

Post by Stela »

Thanks for the answer.

8)
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Die

Post 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.
Post Reply