Name
bind_ param
Synopsis
$rc = $sth->bind_param($p_num, $bind_value) || die $sth->errstr; $rv = $sth->bind_param($p_num, $bind_value, \%attr) || ... $rv = $sth->bind_param($p_num, $bind_value, $bind_type) || ...
The bind_ param
method can be used to bind a value with a placeholder embedded in the prepared
statement. Placeholders are indicated with the question mark
character (?
). For example:
$dbh->{RaiseError} = 1; # save having to check each method call $sth = $dbh->prepare("SELECT name, age FROM people WHERE name LIKE ?"); $sth->bind_param(1, "John%"); # placeholders are numbered from 1 $sth->execute; DBI::dump_results($sth);
Note that the ?
is not enclosed in quotation
marks, even when the placeholder represents a string. Some drivers
also allow placeholders like
:
name
and
:
n
(e.g.,
:1
, :2
, and so on) in addition
to ?
, but their use is not portable. Undefined
bind values or undef
can be used to indicate null
values.
Some drivers do not support placeholders.
With most drivers, placeholders can’t be used for any element of a statement that would prevent the database server from validating the statement and creating a query execution plan for it. For example:
"SELECT name, age FROM ?" # wrong (will probably fail) "SELECT name, ? FROM people" # wrong (but may not 'fail')
Also, placeholders can only represent single scalar values. For example, the following statement won’t work as expected for more than one value:
"SELECT name, age FROM people WHERE name IN (?)" # wrong
Datatypes for ...
Get Programming the Perl DBI 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.