Perl & MySQL summary
Connection
use DBI;
$dbh = DBI->connect('DBI:mysql:databasename;host=db.example.com', 'username', 'password',
{ RaiseError => 1 }
);
Query
$sth = $dbh->prepare('SELECT val FROM exmpl_tbl WHERE id=1');
$sth->execute();
$result = $sth->fetchrow_hashref();
print "Value returned: $result->{val}\n";
Placeholders
$sth = $dbh->prepare('SELECT id FROM exmpl_tbl WHERE val=?',
undef, 'World');
@result = $sth->fetchrow_array();
print "ID of World is $result[0]\n"
Execute and fetch
$sth = $dbh->prepare('SELECT * FROM exmpl_tbl');
$results = $dbh->selectall_hashref('SELECT * FROM exmpl_tbl', 'id');
foreach my $id (keys %$results) {
print "Value of ID $id is $results->{$id}->{val}\n";
}