Database Reference
In-Depth Information
WHERE human_id = ?"
;
$sth = $dbh->
prepare
($sql_stmnt);
$sth->
execute
($membership_expiration,$human_id);
...
Here we're using
shift
twice to capture two values entered by the user and store them
in the
$human_id
and
$membership_expiration
variables. The
$sql_statement
is given two placeholders. We replace those placeholders with the
two variables, in the proper order, when we execute the SQL statement through the state-
ment handle (
$sth
) using
execute()
.
The end result of this bit of code is to update the row related to the given
$human_id
in
the
humans
table. Because this
UPDATE
privilege isone to which you might not want
the public to have access, it would be best to use this program just internally from a
known IP address, and torequire a password.
A Full Example with Perl DBI
It's easier to explain thecomponents of a program step by step as I have done here, but
seeing a program in pieces can be confusing. Combinig these Perl program snippets, we'll
create a program and call it
member_adjust_expiration.plx
. Here's how it looks:
#!/usr/bin/perl -w use strict;
use
DBI
;
my
$search_parameter =
shift
||
''
;
my
$human_id =
shift
||
''
;
my
$membership_expiration =
shift
||
''
;
my
$user =
'admin_members'
;
my
$password =
'doc_killdeer_123'
;
my
$host =
'localhost'
;
my
$database =
'birdwatchers'
;
my
$dbh =
DBI
->
connect
(
"DBI:mysql:$database:$host"
, $user, $password)
||
die
"Could not connect to database: "
.
DBI
->
errstr
;
if
($search_parameter && !$membership_expiration) {
my
$sql_stmnt =
"SELECT human_id,
CONCAT(name_first, SPACE(1), name_last) AS
full_name,
membership_expiration
FROM humans