开发者

mysql script not working for date [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

why isn't this pulling data from 7 days back?

Here is what I have, it keeps returning all reminders regardless of the d开发者_Go百科ate. Can anyone help guide me?

$reminder=$_GET['reminder'];

$date = date('Y-m-d',strtotime("last sunday + INTERVAL 7 DAY "));

$query = ("SELECT * FROM contacts WHERE reminder = '$reminder $date' ORDER BY firstname") or die ('Error: ' .mysql_error()); 

$result=mysql_query($query);

while ($row = mysql_fetch_array($result)) {


strtotime is amazingly powerful, but it can't parse a MySQL date selection syntax. If you want 7 days after last sunday, "sunday" works. You can also do, "last sunday + 7 days".

I don't know what $reminder is (are you sure you need to add the date to the reminder variable?), but this will work for your $date variable:

$date = date('Y-m-d',strtotime("sunday"));

As an aside, this is deadly:

$reminder=$_GET['reminder'];
$query = ("SELECT * FROM contacts WHERE reminder = '$reminder $date' ".
            " ORDER BY firstname");
            // or die ('Error: ' .mysql_error()); <-- line is always true...

Look up Bobby Tables to understand why that first line should be:

$reminder=mysql_real_escape_string( $_GET['reminder'] );


You want:

WHERE reminder = '$reminder' AND date_column = $date


the sql you have written is

SELECT * FROM contacts WHERE reminder = '$reminder $date' ORDER BY firstname

i think it should be like as

SELECT * FROM contacts WHERE reminder = '$reminder' AND date='$date' ORDER BY firstname

but still it depends on your schema so just by printing your query & executing in mysql-prompt or phpmyadmin will give you good idea about troubleshooting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜