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.
The following variable name conventions are used in the DBD/DBI documentation:
Table 4-1. DBI module variable naming conventions
Variable name | Meaning |
---|---|
$dbh | database handle object |
$sth | statement handle object |
$rc | Return code (boolean: true=ok, false=error) |
$rv | Return value (usually an integer) |
@ary | List of values returned from the database, typically a row of data |
$rows | Number of rows processed (if available, else -1) |