Name

DBI::prepare

Synopsis

$statement_handle = $db->prepare($statement);
$statement_handle = $db->prepare($statement, \%unused);

DBI::prepare takes as its argument an SQL statement, which some database modules put into an internal compiled form so that it runs faster when DBI::execute is called. These DBD modules (not DBD::mSQL or DBD::mysql) also accept a reference to a hash of optional attributes. The mSQL and MySQL server do not currently implement the concept of “preparing,” so DBI::prepare merely stores the statement. You may optionally insert any number of `?' symbols into your statement in place of data values. These symbols are known as “placeholders.” The DBI::bind_param function is used to substitute the actual values for the placeholders. The function returns undef if the statement cannot be prepared for some reason.

Example

use DBI;
my $db = DBI->connect('DBI:mysql:mydata','me','mypassword');

my $statement_handle = $db->prepare('SELECT * FROM mytable');
# This statement is now ready for execution.

My $statement_handle = $db->prepare(
'SELECT name, date FROM myothertable WHERE name like ?');
# This statement will be ready for exececuting once the placeholder is filled
# in using the DBI::bind_param function.

Get MySQL and mSQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.