473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


Perl and MySQL

By Blair Ireland
Senior Editor, TheScripts.com

Perl and Your MySQL Databases

With all the hype about PHP and other Server Side Languages lately, most people are starting to forget about the powers that Perl has. Have you checked out CPAN lately? There are so many modules available now, its unbelievable. From parsing incoming e-mail to using ICQ, these modules exist for Perl.

One of the greatest powers a Server Side Language like PHP has in its arsenal is it's connections to MySQL and mSQL. It's just too easy. With functions like mysql_connect(), mysql_query() and mysql_fetch_array(), your life just becomes simple. Believe it or not, with Perl now, its just as easy.

The name of the module, Msql / Mysql, are the Perl interfaces to the mSQL and MySQL databases. Before we really get into it, check it out at http://search.cpan.org/search?module=Mysql.

MySQL Functions

The best way to explain each of these functions is to compare them with their PHP equivalents.

Remember that, before you use any of these functions, you need the line

use Mysql;

in your script, above the use of the functions. Usually I deal with this at the top of the script.

Connecting to the MySQL Server

PHP has:
mysql_connect("hostname","username","password");

Perl has:
$db = Mysql->connect($host, $database, $user, $password);

$db is the pointer to that specific MySQL connection for the duration of the script. You can have multiple calls to Mysql->connect, but, you must use a different scalar variable each time. It's usage should be self-explanatory with the names of the variables......

Selecting your Database

PHP has:
mysql_select_db("database name", $link_id );

Perl has:
$db->selectdb($database);

Notice that I am still using the $db scalar here. Since it is the 'link' to our database connection, we have to make reference to it. $database is the name of the database that you are accessing. It would be a string, and can be directly inserted or put into a variable, like the example.

  Database Interaction »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.