Re: sqlite AND OR query ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, May 8, 2007 7:09 pm, chris@xxxxxxxxxxxx wrote:
> I want to select two dates one being todays date and the other being
> 0000-00-00 which just a date
> in the database when no proper date has been entered.
>
> Ive tried this but it does not work...
>
> $today = date('Y-m-d');
> $result = sqlite_query($db, "SELECT * FROM domains WHERE date =
> '0000-00-00'
> AND '$today'");

First of all, you can't just say "... AND '$today'"

You need to say "... AND date = '$today' "

When you say "... AND '$today' ":

The database just converted $today into a TRUE/FALSE question.

And since the only way to answer a T/F question is with 1/0 in a
computer, it decides that $today is not 0, because it's got numbers
ands stuff in it.

So it's ALWAYS true.

So your query really turned into:

where today = '0000-00-00' and TRUE

The "and TRUE" bit doesn't really do much, you see...

$x AND TRUE === $x

Actually, NULL might turn into false, if you have NULL dates.
And for technical reasons, midnight of January 1, 1970, might turn
into 0...  But ignore that for now, as you're typing something silly
anyway, and you want " where date = '0000-00-00' and date = '$today' "

Next, think about this:

date = '0000-00-00' AND date = '2007-05-08'

Is it possible for any date to be = to both of those values at once?

No.

A date cannot be both 0000-00-00 and also be today as well.

Unless maybe you've invented a time machine... :-)

> I just get items with '0000-00-00' returned or if I reverse it ie...
>
> WHERE date = '$today'AND '0000-00-00'");
>
> I just get  $today items returned.
>
> I also tried using OR instead of AND ie...
>
> WHERE date = '0000-00-00' OR '$today'");
>
> But that seems to be returning all items in the db.

This, of course, turns into:

date = '0000-00-00' OR TRUE

and that is always TRUE:

$x OR TRUE === TRUE


Hope that helps.

Technically, this was all SQL, but it applies equally well in PHP with
&& and any programming language.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux