Page 398 - Beginning PHP 5.3
P. 398

Part III: Using PHP in Practice
                  Making a Connection

                   To make a connection to a MySQL database in your PHP script, all you need to do is create a new  PDO
                 object. When you create the object, you pass in three arguments: the DSN, which describes the database
                 to connect to; the username of the user you want to connect as; and the user ’ s password. The returned
                   PDO  object serves as your script ’ s connection to the database:

                      $conn = new PDO( $dsn, $username, $password );

                  A  DSN , or Database Source Name, is simply a string that describes attributes of the connection such as
                 the type of database system, the location of the database, and the database name. For example, the
                 following DSN can be used to connect to a MySQL database called   mydatabase  running on the same
                 machine as the PHP engine:


                    $dsn = “mysql:host=localhost;dbname=mydatabase”;



                     If  host  isn ’ t specified,  localhost  is assumed.
                  So, putting it all together, you could connect to your   mydatabase  database as follows (replacing  mypass
                 with your real root password of course):
                    $dsn = “mysql:dbname=mydatabase”;
                    $username = “root”;
                    $password = “mypass”;
                    $conn = new PDO( $dsn, $username, $password );
                  When you ’ ve finished with the connection, you should close it so that it ’ s freed up for other scripts to
                 use. Although the PHP engine usually closes connections when a script finishes, it ’ s a good idea to close
                 the connection explicitly to be on the safe side.

                   To close the connection, just assign   null  to your connection variable. This effectively destroys the  PDO
                 object, and therefore the connection:

                    $conn = null;


                  Handling Errors

                   Database errors can be notoriously difficult to track down and deal with. One of the nice things about
                 PDO is that you can get it to return MySQL errors in the form of highly descriptive   PDOException
                objects. You can then use the PHP keywords   try  and  catch  to handle these exceptions easily and deal
                with them appropriately.
                      Exceptions are covered fully in Chapter 20, so you just learn the basics here.

                  To set PDO to raise exceptions whenever database errors occur, you use the   PDO::SetAttribute
                 method to set your   PDO  object ’ s error mode, as follows:

                      $conn = new PDO( $dsn, $username, $password );
                      $conn- > setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );



              360





                                                                                                      9/21/09   9:11:14 AM
          c12.indd   360                                                                              9/21/09   9:11:14 AM
          c12.indd   360
   393   394   395   396   397   398   399   400   401   402   403