Access MySQL from Perl
Now you have got the basic concept about how
to pull information from a database by using PHP. However, there are times you
will want to work with MySQL data outside of the website. For example, if you
want to run analyzes of much data in the database or perform automatically
administrative tasks based on information in a database, you will not want to
do it in PHP. With this type of situations, you will want to use a scripting
language, such as Perl.
you
can use the MySQL client or you can use a perl API for connecting to MySQL from
perl scripts.
While PHP has an extension installed to
interface with MySQL, Perl is much more modular. You have to use many Perl
modules before you can start talking to it. In particular, you need to use DBI
and DBD::mysql modules of Perl before you can start interacting with MySQL.
Fortunately, because MySQL is so popular with open source community, these two
modules are usually available with most versions of Linux.
A simple Perl example
In this example, suppose that you want to
automatically delete any login account of customers who have not accessed
within one year. To do it, you will need a column to track the last time when
the customer logged in. Therefore, suppose that our logins database from
the PHP example has a column called "last_login". With that
information, we can write a Perl script which will delete all user accounts
that have not logged in one year. The script could look like this:
#!/usr/bin/perl
use DBI;
use DBD::mysql;
$db_host = "localhost";
$db_user = "root";
$db_pass = "rootpassword";
$db_name = "store"
$dsn = "DBI:mysql:database=$db_
name;host=$db_host";
$dbh = DBI->connect($dsn,
$db_user, $db_pass);
$expire_sql = "SELECT
COUNT(*) AS count
FROM logins WHERE
last_login <= DATE_
SUB(NOW(), INTERVAL
1 year);
$sth =
$dbh->prepare("$expire_sql");
$sth->execute;
$row = $sth->fetchrow_hashref;
print "There are " .
$row->{"count"} . " accounts that haven’t
been accessed in over a year.\n";
$expire_sql = "DELETE FROM logins
WHERE last_login <=
DATE_SUB(NOW(), INTERVAL 1 year);
$sth =
$dbh->prepare("$expire_sql");
$sth->execute;
$sth->finish;
$dbh->disconnect;
If you consider PHP code from our previous
example, you can see some similarities in the Perl code above. Because the
database interface of Perl is more modular than PHP, it has the concept of a
Data Source Name (DSN) to indicate which database module of Perl is used. Thus,
when you're using Perl to talk to any database, you must specify a DSN to apply
to the database you want to talk, with appropriate parameters for that
database. Each database has its own DSN format, but the format of the MySQL is
just which Perl module, MySQL server is running, and which database is used.
Then the code is very similar to PHP code presented above, with the more
advanced SQL code are included.
The first $ expire_sql string asks MySQL to
count the number of cases where an entry in logins table matches WHERE
criteria. In this case, we told MySQL to use preset date function DATE_SUB(), try to
reduce one year to the current date, as indicated by NOW (). MySQL will
return a number that can be referenced by the count column. We notice
how many users we intend to delete and then we create a different SQL query
which automatically deletes these columns. After completing the deletion, the
script closes the database connection and exit.
Because it’s not enough, we can only cover
about it on the website upgrade by adding a MySQL database for it, but
hopefully we have given you some basic examples to help you start.