Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1

foreach $pair(@pairs){


($name, $val) = split(/=/,$pair);


$val =~ tr/+/ /;


$val =~ s/%([a-fA-f0-9][a-fA-F0-9])/pack("C", hex($1))/eg;


$name =~ tr/+/ /;


$name =~ s/%([a-fA-f0-9][a-fA-F0-9])/pack("C", hex($1))/eg;


$searchField{$name} = $val;


}


}


return (%searchField);


}


print "Content-Type: text/html\n\n";


print "Record Added";


print "The Vendor has been added";


print "";


The first line of this listing tells where to find the Perl interpreter. The next line tells the interpreter to use
the DBI module. There is also a CGI module that does a lot of neat things. You can find this out on
CPAN as well.
$database = "Meet_A_Geek";
$driver ="mysql";

$dsn = "DBI:$driver:database=$database;$options";
$dbh = DBI->connect($dsn,"root",'tacobell')
or die "Error connecting to database";
This piece of code builds the connection string. Notice that a third variable ($DSN) is used to store the
driver and database information. It was done this way to make the code easier to read.
%postInputs = readPostInput();
This line of code calls the readPostInput() function. This function parses the name/value pairs and
creates a hash that contains these values.
Free download pdf