4.5. DBI/DBD syntax

The syntax of the database modules is best found by using the perldoc command. perldoc DBI will give you general information applicable to all DBI scripts, while perldoc DBD::yourdatabase will give information specific to your own database. In our case, we use perldoc DBD::mysql.

DBI is an object oriented Perl module, like the Text::Template and Mail::Mailer modules covered in the CGI Programming in Perl training module. This means that when we connect to the database we will be creating an object which is called a "database handle" which refers to a specific session with the database. Thus we can have multiple sessions open at once by creating multiple database handles.

We can also create statement handle objects, which are Perl objects which refer to a previously prepared SQL statement. Once we have a statement handle, we can use it to execute the underlying SQL as often as we want.

4.5.1. Variable name conventions

The following variable name conventions are used in the DBD/DBI documentation:

Table 4-1. DBI module variable naming conventions

Variable nameMeaning
$dbhdatabase handle object
$sthstatement handle object
$rcReturn code (boolean: true=ok, false=error)
$rvReturn value (usually an integer)
@aryList of values returned from the database, typically a row of data
$rowsNumber of rows processed (if available, else -1)