Makke_ Posted May 30, 2013 Share Posted May 30, 2013 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); ?> Quote Link to comment Share on other sites More sharing options...
Makke_ Posted May 30, 2013 Author Share Posted May 30, 2013 (edited) If I hardwrite the date like this '2013-05-29' in the query that works.If I echo the $date->format('Y-m-d') I get the date in correct format. Edited May 30, 2013 by Makke_ Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted May 30, 2013 Solution Share Posted May 30, 2013 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. Quote Link to comment Share on other sites More sharing options...
Makke_ Posted May 30, 2013 Author Share Posted May 30, 2013 That solved it, thanks!I used this '{$date->format('Y-m-d')}' with the quotes in the query. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.