http://qs321.pair.com?node_id=309947

shockme has asked for the wisdom of the Perl Monks concerning the following question:

I have code such as the following:
my $numParams = 1; my $SQL = 'SELECT syslog_timestamp, syslog_host, syslog_text FROM syslog WHERE syslog_text like ?'; if ($begMonth) { $begDate = "$begYear\-$begMonth\-$begDay 00:00:00"; $endDate = "$endYear\-$endMonth\-$endDay 23:59:59"; $SQL .= ' AND syslog_timestamp >= ?'; $SQL .= ' AND syslog_timestamp <= ?'; $numParams += 2; } my $sth = $dbh->prepare($SQL); if ($numParams == 1) { my $DBquery= $sth->execute($SearchPhrase); } elsif ($numParams == 3) { my $DBquery= $sth->execute($SearchPhrase, $begDate, $endDate); }
I've narrowed the following problem down to the involvement of $begDate and $endDate. Here's what's happening:

When I execute this, it always returns 0 rows. I've omitted some code which displays the contents of $SQL to me, as well as code which displays the contents of $begDate and $endDate. When I cut and paste the contents of $SQL and the values of $begDate and $endDate into the MySQL commandline, it returns over 1,000 rows (which is what I would expect). If I don't give it $begDate or $endDate, it works fine. However, any time I use the date logic, it returns 0 rows and no errors.

Also, if I omit the placeholders and construct the following:

if ($begMonth) { $begDate = "$begYear\-$begMonth\-$begDay 00:00:00"; $endDate = "$endYear\-$endMonth\-$endDay 23:59:59"; $SQL .= " AND syslog_timestamp >= '$begDate'"; $SQL .= " AND syslog_timestamp <= '$endDate'"; }
It works like a charm, returning in excess of 1,000 rows.

So, I'm apparently missing something obvious in the placeholder construction/execution. Any suggestions?

(Note: Typos in the above are due to cut and paste errors.)

If things get any worse, I'll have to ask you to stop helping me.