开发者

Mysql query parameter issue in Perl

Got a little problem in my code, the query works fine if I manually put values in. But failed if I use my variable. The code shows below

my $get_meter_id = $dbh->prepare("SELECT * from t_readings where meter_serial = '21001652' AND ...");
$get_meter_id->execute() or die "Couldn't execute statement: ".$get_meter_id->开发者_如何学编程errstr;
my $meter_reg_id = $get_meter_id->fetchrow_array();

Above one works

where meter_serial = 21001652 AND ...")

Above one works.

where meter_serial = '".$variable."' AND ...")

Above doesn't work

where meter_serial = ".$variable." AND ...")

Above doesn't work

Many thanks.


Use placeholders. Don't fiddle about with string concatenation.

my $get_meter_id = $dbh->prepare("SELECT * from t_readings where meter_serial=? AND ...");
my $foo = 21001652;
$get_meter_id->execute($foo) or die "Couldn't execute statement: ".$get_meter_id->errstr;


What about:

my $get_meter_id = $dbh->prepare("SELECT * from t_readings where" .
     "meter_serial = ? AND ...");
$get_meter_id->execute($variable) or die "Couldn't execute statement: " . 
     $get_meter_id->errstr;
my $meter_reg_id = $get_meter_id->fetchrow_array();

further reading

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜