while working on Dancer app I try to use placeholders for the SQL queries. The first code gives a good result but it is without placeholders. The second with placeholders doesn't show the result I had expected. To my idea the problem is with the third placeholder $link. Can't find out what am I doing wrong?
Thanks,
GertFirst code without placeholders but it works
prefix '/en/collections' => sub { get '/:link' => sub { my $link = params->{'link'}; my $sql = "SELECT imgf, long_i, status_prod, name_prod, name_cat, name_col FROM products, categories, collections WHERE products.fk_cat = categories.id_cat AND products.fk_col = collections.id_col AND link_col= '$link'"; my $sth = database->prepare($sql); $sth->execute; template 'col-m', { have_text_markdown => $have_text_markdown, bigprodlist => $sth->fetchall_arrayref( {} ) }, { layout => 'pin' }; }; };
Second code without placeholders but does not work?
prefix '/en/collections' => sub { get '/:link' => sub { my $link = params->{'link'}; #print Dumper $link; my $sql = <<'SQL'; SELECT imgf, long_i, status_prod, name_prod, name_cat, name_col FROM products, categories, collections WHERE products.fk_cat = ? AND products.fk_col = ? AND link_col = ? SQL my $sth = database->prepare($sql); #print Dumper $link; # is okay $sth->execute( 'categories.id_cat', 'collections.id_col', $link ); template 'col-m', { have_text_markdown => $have_text_markdown, bigprodlist => $sth->fetchall_arrayref( {} ) }, { layout => 'pin' }; }; };
In reply to Avoid embedding variables in SQL - Dancer App by GertMT
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |