Problem with Mysql query parameter in Perl

Got a little problem in my code, the query works fine if I manually entered the values. But failed if I use my variable. Below is the code

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(); 

Working on one

 where meter_serial = 21001652 AND ...") 

Above works.

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

Above does not work

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

Above does not work

Many thanks.

+4
source share
2 answers

Use placeholders. Do not play 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; 
+7
source

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

+5
source

Source: https://habr.com/ru/post/1314041/


All Articles