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.


In reply to MySQL Placeholder Date Problem by shockme

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.