Previous Page Next Page

Chapter 17. Perl Meets MySQL—A Perfect Connection

17.1. Introduction

The user has filled out a form and submitted it with a list of items he wants to purchase. Information for that user is stored in a database in a table called "customers." You want to open the database and add the new order directly from your Perl program. And you may want to retrieve all the previous orders and product information for that customer and format the data for a Web page, use it in an e-mail message, and send it to a spreadsheet, etc., also from a Perl program. This is all possible with Perl and the Perl DBI module, an object-oriented database interface that allows you to connect to any relational database and use Perl methods to perform all the necessary operations for opening and closing the database, for sending SQL queries to create tables, update and delete them, retrieve and modify records, manage transactions, and display results.

This chapter focuses on using Perl with the MySQL database, a very popular open source, fully functional, relational database. You will learn how to issue commands at the MySQL client and then use the DBI module to issue the same commands from a Perl script. Finally, we will tie all of this together by creating a dynamic Web page using both the DBI module and CGI.pm.

The subject of databases is huge. This chapter is not an attempt to teach you how to correctly design the structure of a database or the best practices for organizing the data. That would take another book or more, so if you are a complete novice, have never been exposed to databases and how they work, you might find Databases Demystified by Andy Oppel an excellent tutorial for getting started.[1] This chapter will cover the basic concepts and terminology you will need in order to work with the Perl DBI and MySQL.

[1] Oppel, Andrew J. Databases Demystified. McGraw-Hill/Osbourne, Emeryville, CA, 2004.

Previous Page Next Page