Database Reference
In-Depth Information
Perl DBI
The easiest method ofconnecting to MySQL with the Perl programming language is to use
the Perl DBI module. This section assumes that you have a basic knowledge of the Perl
language. We'll focus on how to connect to MySQL, run SQL statements, and retrieve data
with Perl, rather than the idiosyncrasies of Perl itself. This is meant to be a simple tutorial
for a Perl programmer to get started with the Perl DBI.
For the example in this section, suppose we want to write a program for one of the adminis-
trators to get a list of members and to optionally change the expiration of their membership.
For this, we'll use the admin_members user account that's designated for administering
information on members. We created that user account at the start of this chapter.
Installing
The Perl DBI module ispart of the core Perl installation. You can download both Perl and
the DBI module from CPAN .
If your server already has Perl installed on it, which most do, you can execute the following
from the command line to install the DBI module:
perl - MCPAN - e 'install DBI'
If you don't have Perl installed already on your server, you can use an installation utility
like yum to install the DBI module. If you have yum on your server, enter the following
from the command line while logged in as root or an administrative filesystem user:
yum install perl perl-mysql
Connecting to MySQL
To interface withMySQL, you must first call the DBI module and then connect to MySQL.
To make a connection to a database using the Perl DBI, only the following lines are needed
in a Perl program to connect to the database:
#!/usr/bin/perl -w
use strict ;
use DBI ;
my $user = 'admin_members' ;
my $password = 'doc_killdeer_123' ;
my $host = 'localhost' ;
my $database = 'birdwatchers' ;
Search WWH ::




Custom Search