Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

MySQL Placeholder Date Problem

by shockme (Chaplain)
on Nov 25, 2003 at 16:31 UTC ( #309947=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on MySQL Placeholder Date Problem
Select or Download Code
Re: MySQL Placeholder Date Problem
by Abigail-II (Bishop) on Nov 25, 2003 at 16:36 UTC
    If you turn on tracing in the DBI, you can see what exactly is being send to the server.

    (Note: Typos in the above are due to cut and paste errors.)
    Then how are we supposed to know what's a cut-and-paste typo, and what's a bug in the real code?.

    Abigail

      Fair enough. I guess the point I was trying to make was that the code compiles and runs, so in the event I've incorrectly pasted a ; or ", which would cause the code to not compile, then that can be overlooked.

      But I get your point, and you are correct.

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

        If it's a true cut-n-paste from your code there shouldn't really be any typos - unless your original code had typos of course. Maybe you're re-typing the code by hand, in which case typos can and do frequently crop up.

        -- vek --
      And thanks for the advise on tracing. That helped me pinpoint the error exactly!

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

        What WAS the error?

        Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: MySQL Placeholder Date Problem
by runrig (Abbot) on Nov 25, 2003 at 21:53 UTC
    You already have your answer (although we don't yet know what it was), but you can reduce your logic by doing something like this:
    my @args = ($SearchPhrase); ... if ($begMonth) { ... $SQL .= " AND syslog_timestamp BETWEEN ? AND ?"; push @args, $begDate, $endDate; } ... $sth->execute(@args);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://309947]
Approved by mpeppler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-10-21 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls