Using the Die Statement

I l @ ve RuBoard

A final type of error you will encounter is the circumstantial error. These occur only when something else on the server goes wrong. Fortunately, they are more rare than programming errors. For example, if the MySQL database isn't running, every database- related function will generate an error. Or, as you saw with the email.php example, if the server's sendmail application isn't working (on a Unix server), that would cause a problem as well. Although these system errors are beyond the control of the programmer, you still ought keep their possible occurrence in mind as you code.

You can stop these errors in their tracks by using the die statement. The die statement tells the PHP to cease execution of a script when an error occurs. It can also print an error message to the browser or call a function. For example,

 $Link = mysql_connect ($Host, $User,  $Password) or die ("Couldn't connect to  the database."); 

If, for any reason, PHP is not able to connect to the database, the script will stop executing and the Couldn't connect to the database message will be printed.

 $Link = mysql_connect ($Host, $User,  $Password) or die (mysql_error()); 

In this example, should PHP not be able to connect to the database, die will call the mysql_error() function. The mysql_error() function prints the actual error that he MySQL database generates.

Both of the above examples work because if PHP determines that the first part of the conditional (i.e., mysql_connect ) is FALSE, it will enact the second part of the conditional (which is the die statement) to guarantee that the entire line evaluates to TRUE. In other words, the line says if the connection to the database doesn't work, do this. If the connection to the database does go through, then that's all that is required.

As an example of this, you'll add the die statement to a very basic database script that connects to MySQL and then tries to select the database.

To use the die statement:

  1. Create a new PHP document in your text editor.

  2. Begin with the standard HTML head (Script 14.6):

     <HTML> <HEAD> <TITLE>Die!</TITLE> <BODY> 
    Script 14.6. For scripts that use several lines of code, each of which relies upon the successful completion of its predecessor, the die statement is invaluable in reducing and handling errors. Since die is managing potential errors, PHP's built-in error reporting has also been turned off (compare Figure 14.11 with Figure 14.13).

    graphics/14sc06.jpg

  3. Open the PHP section with the initial PHP tag and then set the variables for the database access.

     <?php $Host = "localhost"; $User = "username"; $Password = "password"; $DBName = "database"; 
  4. Turn off all error reporting.

     error_reporting(0); 

    Since the die statement will manage the errors that occur, you'll turn off PHP's default method of reporting errors to avoid redundancy.

  5. Connect to MySQL.

     $Link = mysql_connect ($Host, $User,  $Password) or die("Couldn't connect  to the database!"); 

    This line of code tells PHP to attempt to connect to MySQL. If it cannot do so for any reason, it will print the Couldn't connect to the database message and stop execution of the script. It is important to stop the execution immediately, because if PHP cannot connect to the database, the next two lines of code, which rely on that connection, will also cause error messages. (In actuality, by turning error_reporting off, no error will be reported although errors will still occur. Regardless of this fact, it's best to stop problem scripts at the first possible instant.)

  6. Select the database.

     mysql_select_db ($DBName, $Link) or  die(mysql_error()); 

    Here, if PHP cannot select the database (determined by $DBName), it will cease execution of the page and print the error generated by MySQL.

  7. Close the MySQL link and the PHP section.

     mysql_close ($Link); ?> 
  8. Create a simple message and finish the HTML page.

     Testing the die statement! </BODY> </HTML> 

    I have added this line of text so that the page has some content on it. This page could easily be modified to query a database as well.

  9. Save your script as die.php, upload it to the server, and test in your Web browser (Figures 14.11, 14.12, and 14.13).

    Figure 14.11. By turning off error reporting and using the die statement, a more meaningful error message can be displayed to the user. Also, the script will cease execution of the page since it could not complete this first step.

    graphics/14fig11.gif

    Figure 14.12. Here the die statement calls the mysql_error() function, which prints the actual error message the MySQL database generated.

    graphics/14fig12.gif

    Figure 14.13. If error reporting is not turned off, the end user would see both the PHP error message as well as the message created by the die statement. Compare this to Figure 14.11 where error reporting has been turned off.

    graphics/14fig13.jpg

Tip

You can also prevent error messages by using the at symbol (@) before any function call. Referred to as the error-control operator, the at symbol will disregard any errors caused by the function it precedes. It is used like so:

 $Link = @mysql_connect ($Host, $User, $Password); 

Unlike die, @ does not give you the option of printing a message or calling a function.


Tip

You can also use die to call your own functions. For example, if you have a function created for the purposes of printing error messages in a particular format, die could call it.

 $Link = mysql_connect ($Host, $User, $Password) or die (print_message ("Couldn't connect.")); 

I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net