Chapter 5 — Storing and Sharing Information 89
Listing 5-26: Inserting Data with PHP (PDO)
<?php
try {
$dbh = new PDO(‘mysql:host=localhost;dbname=google_maps’,’maps’,’maps’);
} catch (PDOException $e) {
print “Error!: “. $e->getMessage(). “
”;
die();
}
$dbh->exec(“insert into restaurants values(0,-0.6391666,52.911111,’China
Inn’)”);
$dbh = null;
?>
Although more detailed examples of inserting and updating the database are beyond the scope
of this book, examples are provided as part of the applications presented in the remainder of
the book.
Extracting Data from the Database
When extracting data from the database, the typical procedure is to execute the SELECTstate-
ment and then iterate over the returned rows to extract the information required. The best way
to achieve this is through the use of a prepared statement. This provides the most flexible
method for extracting information from the database on a row-by-row basis.
An example of this through the Perl/DBD interface is shown in Listing 5-27.
Listing 5-27: Extracting Data on a Row-by-Row Basis
#!/usr/bin/perl
use DBI;
use Data::Dumper;
my $dbh = DBI-
connect(“DBI:mysql:database=google_maps;host=mysql.mcslp.pri”,’maps’,’maps’);
if ($dbh)
{
Continued