I've got the following Perl code which makes a DBI call:
my $artsql = q{ *** SNIP A BUNCH OF SQL ***
where a.article_id != ?
and at.type_name != 'List Element' -- don't get list children
and aw.flowstate = 'Published'
and a.visible_as_article = 1 }
. ( $filter ? q{and ch.channel_id = ?
and cat.category_id = ? }
: '' )
. q{order by a.publish_date desc
limit 5};
my @bind = ( $article );
push @bind, ( $channel_id, $category_id ) if $filter;
my $articles = $dbh->selectall_arrayref( $artsql, { Slice => { } }, @bind );
When $filter
is on, this code was dying with the error:
DBD::mysql::db selectall_arrayref failed: called with 3 bind variables when 1 are needed
At first I thought this was a problem with the ternary conditional in the middle of the string, (I've been bitten by that bug multiple times) but it was correct. Dumping some debug values shows that the query and @bind
array were being constructed correctly.
I then noticed that the query had a SQL comment right after the first bind variable, so on a whim I removed it. Poof, it worked!
According to the MySQL docs on comments,
MySQL Server supports three comment styles: From a “#” character to the end of the line. From a “-- ” sequence to the end of the line. In MySQL, the “-- ” (double-dash) comment > style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on).
Since the comment had --
followed by a space and (presumably) ended with the end of the line, why did MySQL choke? Is DBI doing something weird with newlines or whitespace behind the scenes?