Perl MySQL placeholder issue

RESOLVED: But I’m leaving this post here for anyone else who may have a similar headache.
The ‘resolution’ was found when I realised I had queried the wrong table so there was no value when I was expecting there to be one. It didn’t error out because I have two cols of the same name in two separate tables; a hangover from pre-normalisation when a col was moved to another table.

The answer is in the second block of code below.

My query works correctly when all my placeholders variables are set to ‘=’ but, I want to compare some columns data with ‘like’.

my $search_param = 'x94';

my $sth = $connect->prepare("
   select id
        , stock_code 
     from table 
    where stock_code = ? 
") or die "stock_code prepare statement failed: $DBI::errstr\n";

$sth->execute($search_param) or die "execute stock_code statement failed: $DBI::errstr\n";

That works as expected but I need find the stock_code where ‘x94’ is within it so I am trying to to ‘%x94%’

EG stock could be “4116 x94p”;

So I have tried

my $id = '123';
my $search_param = 'x94';
my $like_search_param = "\%$search_param\%";
my $sth = $connect->prepare("
   select id
           , stock_code 
     from table 
   where stock_code like ? 
") or die "stock_code prepare statement failed: $DBI::errstr\n";

$sth->execute( $id
             , $like_search_param 
             ) or die "execute statement failed: $DBI::errstr\n";

The query does not error out. it returns no results yet there are results in the db that are like ‘%x94%’

Seeking any advice you may be able to offer, please.

Bazz

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.