1and1 Help Centre Categories

print article

Connect to a MySQL Database Using Perl

For Shared hosting and Windows packages

This is a sample Perl script that connects to a MySQL database and queries the "address" table to display data from the "name" and "email" columns.

#!/usr/bin_uk/perl
use DBI;
print "Content-type:text/html\n\n";

$db_handle = DBI->connect("dbi:mysql:database=dbxxxxxxxx;host=dbxxx.oneandone.co.uk;user=dboxxxxxxxx;password=xxxxxxxx")
or die "Couldn't connect to database: $DBI::errstr\n";

$sql = "SELECT * FROM puretest";
$statement = $db_handle->prepare($sql)
or die "Couldn't prepare query '$sql': $DBI::errstr\n";

$statement->execute()
or die "Couldn't execute query '$sql': $DBI::errstr\n";
while ($row_ref = $statement->fetchrow_hashref())
{
print "Name <b>$row_ref->{name}</b> has e-mail address::<b>$row_ref->{email}</b>.<br>";
}

$db_handle->disconnect();
For additional information, you may want to reference: