timmah1 Posted January 19, 2009 Share Posted January 19, 2009 I have a table in the database named posted in the date format 0000-00-00 Is this possible to do? <?php $month = date("m"); $query = "SELECT * FROM charts WHERE username = 'admin' AND posted = '".date("m", strtotime($month))."'"; $chart = mysql_query($query); $numorws = mysql_num_rows($chart); if($numrows == 0){ echo "nothing"; } while($charts = mysql_fetch_assoc($chart)){ echo $charts['id']."<br />"; } ?> It is returning "nothing", even though there are two items in the db with the posted being 2009-01-14 2009-01-19 Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/ Share on other sites More sharing options...
JonnoTheDev Posted January 19, 2009 Share Posted January 19, 2009 Should be $query = "SELECT * FROM charts WHERE username = 'admin' AND MONTH(posted) = '".date("m", strtotime($month))."'"; Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740512 Share on other sites More sharing options...
timmah1 Posted January 19, 2009 Author Share Posted January 19, 2009 It is still coming back with "nothing" Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740515 Share on other sites More sharing options...
timmah1 Posted January 19, 2009 Author Share Posted January 19, 2009 I got it to work using $query = "SELECT * FROM charts WHERE username = 'admin' AND MONTH(posted) = '$month'"; Since I already declared the month value above Thanks niel Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740518 Share on other sites More sharing options...
JonnoTheDev Posted January 19, 2009 Share Posted January 19, 2009 Ah, you are using strtotime for no reason use: $query = "SELECT * FROM charts WHERE username = 'admin' AND MONTH(posted) = '".date("m", time())."'"; Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740520 Share on other sites More sharing options...
phparray Posted January 19, 2009 Share Posted January 19, 2009 neil.johnson is correct you are using strtotime for no reason. You also are using the wrong date parameter to set the month var. Use $month = date('n'); #this is the equivalent to mysql month() $query = "SELECT * FROM charts WHERE username = 'admin' AND month(posted) = ".$month; Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740540 Share on other sites More sharing options...
timmah1 Posted January 19, 2009 Author Share Posted January 19, 2009 phparray, I'm using the correct var for the month <?php date("m"); #numerical representation of month WITH leading zeros date("n"); #numerical representation of month WITHOUT leading zeros ?> And if you look at my first post, there are leading zeros Thanks Neil for the help Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740548 Share on other sites More sharing options...
phparray Posted January 19, 2009 Share Posted January 19, 2009 date("n"); #numerical representation of month WITHOUT leading zeros And if you look at my first post, there are leading zeros Exactly - using the mysql function month() will output from a date field WITHOUT leading zeros. So using the php function date('n') will be a direct comparison to month(posted) on your table. Quote Link to comment https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740554 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.