You are Here:   FAQ->Scripting & Programming->Perl->Article #2


How do I connect to a MySQL database using Perl?


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


#!/usr/bin/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 help on finding your database information, please reference Where do I find the necessary information to connect to MySQL Database?


Disclaimer: 1&1 provides the scripts and related information on this page as a courtesy, subject to 1&1's General Terms and Conditions of Service (the "GT&C"). As set forth in more detail in the GT&C, the scripts and information are provided "as-is", without any warranty, and 1&1 is not liable for any damages resulting from your use of the scripts or information.



Print Article
How useful was this article?
(From 5 = Very Useful to 1 = Not Very Useful at all):
1 2 3 4 5