The
quote() method is database dependent.
I made this quick test for a MySQL database, and I got
the correct result with both trace(2) and trace(0).
If I change 'joe' into
undef, then I get NULL in both cases.
#!/usr/bin/perl -w
use DBI;
use strict;
my $hDB = DBI->connect("DBI:mysql:test;host=localhost"
. ";mysql_read_default_file=$ENV{HOME}/.my.cnf",
undef, undef, {RaiseError => 1})
or die "can't connect\n";
my $Vars = { 'userid' => 'joe', 'password' => 'secret'};
DBI->trace ( 0 );
my $UserID = $hDB->quote ( $Vars->{ userid } );
my $Password = $hDB->quote ( $Vars->{ password } );
DBI->trace ( 0 );
print "$UserID $Password\n";
$hDB->disconnect();
__END__
'joe' 'secret'
Could you make a similar case script? The culprit could be something else.
quote() will return NULL when your value is undef. I suspect that your values are either undef or becoming undefined somehow along the processing flow.
update
After
talexb's
example, I was able to reproduce the problem and indeed it seems that only by stirring the variable you can get the wanted result.
One of my favorite idioms for quoting (when I am not using placeholders) is to
map-quote the variables from an array.
The following example gives the right result, although it only works with the interpolated "$_".
my ($UserID, $Password) = map {$hDB->quote( "$_" )}
($Vars->{userid}, $Vars->{password});
update (2)
Also a simple interpolation without map tricks will do the job.
my $UserID = $hDB->quote ( "$Vars->{'userid'}" );
my $Password = $hDB->quote ( "$Vars->{'password'}" );
It solves the problem, but doesn't satisfy my curiosity ... ;)
update (3)
Although the quote() method is database dependent, in this case it is not, since the responsible for this strange return is before any other method could be called.
# from the DBI.pm module
1126 sub quote {
1127 my ($dbh, $str, $data_type) = @_;
1128
1129 return "NULL" unless defined $str;
# more code follows .....
This code will return NULL if the string passed is undefined. However, the string
is defined, as
I could check with a simple print.
Just brainstorming ...
_ _ _ _
(_|| | |(_|><
_|
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.