Database Reference
In-Depth Information
of any begin block) are caught by the default exception handler, which prints a stack
trace and exits. Recipe 2.2 discusses error handling further.
Additional connection parameters. To specify the path to a socket file for localhost con‐
nections on Unix, provide a socket option in the DSN:
dsn = "DBI:Mysql:host=localhost;database=cookbook" +
";socket=/var/tmp/mysql.sock"
To specify the port number for non- localhost (TCP/IP) connections, provide a port
option:
dsn = "DBI:Mysql:host=127.0.0.1;database=cookbook;port=3307"
PHP
To write PHP scripts that use MySQL, your PHP interpreter must have MySQL support
compiled in. If your scripts are unable to connect to your MySQL server, check the
instructions included with your PHP distribution to see how to enable MySQL support.
PHP actually has multiple extensions that enable the use of MySQL, such as mysql , the
original (and now deprecated) MySQL extension; mysqli , the “MySQL improved” ex‐
tension; and, more recently, the MySQL driver for the PDO (PHP Data Objects) inter‐
face. PHP scripts in this topic use PDO. To obtain PHP and PDO if they're not already
installed, see the Preface .
PHP scripts usually are written for use with a web server. I assume that if you use PHP
that way, you can copy PHP scripts into your server's document tree, request them from
your browser, and they will execute. For example, if you run Apache as the web server
on the host localhost and you install a PHP script named myscript.php at the top level
of the Apache document tree, you should be able to access the script by requesting this
URL:
http://localhost/myscript.php
This topic uses the .php extension (suffix) for PHP script filenames, so your web server
must be configured to recognize the .php extension (for Apache, see Recipe 18.2 ).
Otherwise, when you request a PHP script from your browser, the server simply sends
the literal text of the script and that's what appears in your browser window. You don't
want this to happen, particularly if the script contains the username and password for
connecting to MySQL.
PHP scripts often are written as a mixture of HTML and PHP code, with the PHP code
embedded between the special <?php and ?> tags. Here is an example:
< html >
< head >< title > A simple page </ title ></ head >
< body >
< p >
Search WWH ::




Custom Search