Forum Moderators: coopster

Message Too Old, No Replies

MySQL Query with PHP variables

         

Sarah Atkinson

6:24 pm on May 6, 2005 (gmt 0)

10+ Year Member




My Query is like this I have two dates inter in by the user and set to $x(lower) and $z(higher). I want to pull all entries that are between these 2 dates.

This does not work (Why?):
$result = @mysql_query("SELECT id, maincourse, veggie, veggie2, fruit, bread, dessert, drink, DATE_FORMAT(day, '%a %b, %D') as date_string FROM $fromwhere WHERE day<=$x AND day<=$z");

note: "WHERE day>$x" does work.

yellow_nemo

6:28 pm on May 6, 2005 (gmt 0)

10+ Year Member



since $x is lower and $z is higher, it should be:

.......where day>=$x and day<=$z

not sure why day>$x wouldn't work in your case.

Sarah Atkinson

6:31 pm on May 6, 2005 (gmt 0)

10+ Year Member



since $x is lower and $z is higher, it should be:

.......where day>=$x and day<=$z

my mistake when I coppied it onto the board I got them mixed up

Actual code is "day>=$x AND day<=$z " and It doesn't work.

Sarah

yellow_nemo

6:33 pm on May 6, 2005 (gmt 0)

10+ Year Member



and $x and $y are integers?

If they are not integers, then I believe you have to put single quotes around them: '$x' and '$y'.

Sarah Atkinson

8:42 pm on May 6, 2005 (gmt 0)

10+ Year Member



$x & $z are dates.. are dates defined as strings or ints? I think strings so mybe that is the problem.

$x = date($_GET['xdate']);

(or is the Date all caps(I don't have the code right in front of me, I'm currently at home and not work the problem just followed me home and I can't get my mind off it)

xdate is entered into the form like (2005-05-05) or (05-5-5) or (5-5-5)

then past with the URL.

ramoneguru

10:59 pm on May 6, 2005 (gmt 0)

10+ Year Member



$fromwhere = MyTable;
$result = @mysql_query("SELECT id, maincourse, veggie, veggie2, fruit, bread, dessert, drink, DATE_FORMAT(day, '%a %b, %D') as date_string FROM $fromwhere WHERE day <='$x' AND day <='$z'");

This query assumes $fromwhere is set to the table name of the table you want to select from. Also, you need to put the PHP values in single parenthesis like I have above.
--Nick