Database Reference
In-Depth Information
<? php
print ( "I am PHP code, hear me roar!" );
?>
</p>
</body>
</html>
For brevity in examples consisting entirely of PHP code, typically I'll omit the enclosing
<?php and ?> tags. If you see no tags in a PHP example, assume that <?php and ?>
surround the entire block of code that is shown. Examples that switch between HTML
and PHP code do include the tags, to make it clear what is PHP code and what is not.
PHP can be configured to recognize “short” tags as well, written as <? and ?> . This topic
does not assume that you have short tags enabled and does not use them.
The following PHP script, connect.php , connects to the MySQL server, selects cook
book as the default database, and disconnects:
<? php
# connect.php: connect to the MySQL server
try
{
$dsn = "mysql:host=localhost;dbname=cookbook" ;
$dbh = new PDO ( $dsn , "cbuser" , "cbpass" );
print ( "Connected \n " );
}
catch ( PDOException $e )
{
die ( "Cannot connect to server \n " );
}
$dbh = NULL ;
print ( "Disconnected \n " );
?>
To try connect.php , locate it under the api directory of the recipes distribution, copy
it to your web server's document tree, and request it using your browser. Alternatively,
if you have a standalone version of the PHP interpreter for use from the command line,
execute the script directly:
% php connect.php
Connected
Disconnected
For background on running PHP programs, read “Executing Programs from the Com‐
mand Line” on the companion website (see the Preface ).
$dsn is the data source name (DSN) that indicates how to connect to the database server.
It has this general syntax:
driver : name=value ; name=value ...
Search WWH ::




Custom Search