Jump to content

SQL with PHP Date not working


Makke_

Recommended Posts

I have a SQL query where I am retrieving information to a table.
I would like to only retrieve data from yesterday.
I found some PHP code which I´ve formated to look exactly as the Date does in the database (2013-05-30) YYYY-mm-dd but I can´t get it to work.

Does someone see what the error might be?

<?php
$date = new DateTime();
$date->add(DateInterval::createFromDateString('yesterday'));

$conn = mysql_connect($dbhost, $dbuser, $dbpassword);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db($dbname, $conn);
echo '<STYLE TYPE="text/css">';
echo 'TD{font-family: Arial; font-size: 9pt;}';
echo '</STYLE>';
echo '<table border=1><thead><tr><th>Namn</th><th>Datum</th><th>SE Mail</th><th>SE Backoffice</th><th>DK Mail</th><th>DK Backoffice</th><th>NO Mail</th><th>NO Backoffice</th><th>FI Mail</th><th>FI Backoffice</th></tr></thead><tbody>';
$q = mysql_query("SELECT id, name, Date, SEMail, SEBackoffice, DKMail, DKBackoffice, NOMail, NOBackoffice, FIMail, FIBackoffice FROM maildata WHERE Date = $date->format('Y-m-d')");
while($f = mysql_fetch_array($q)) {
    echo '<tr><td>'.$f['name'].'</td><td>'.$f['Date'].'</td><td>'.$f['SEMail'].'</td><td>'.$f['SEBackoffice'].'</td><td>'.$f['DKMail'].'</td><td>'.$f['DKBackoffice'].'</td><td>'.$f['NOMail'].'</td><td>'.$f['NOBackoffice'].'</td><td>'.$f['FIMail'].'</td><td>'.$f['FIBackoffice'].'</td></tr>';
}
echo '</tbody></table>';
mysql_close($conn);
?>
Link to comment
https://forums.phpfreaks.com/topic/278591-sql-with-php-date-not-working/
Share on other sites

because you are using a class method, that also contains quotes as part of its syntax, inside of a php string, you need to enclose it in { } so that php can properly parse all of it - {$date->format('Y-m-d')}

 

if you had php's error_reporting/display_errors turned on you would be getting a notice message at the use of $date->format('Y-m-d') because php thinks it is a reference to a property, not a reference to a method because php needs to be told that the ('Y-m-d') is part of the variable instead of being part of the string it is in.

 

you should also form sql query statements in php variables so that you can echo them out to see exactly what they are for debugging purposes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.