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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With