timmah1 Posted July 15, 2009 Share Posted July 15, 2009 I have a field in my database called purchase. I is formatted like 2009-01-30 I need to select items from the form with only the month and the year, depending on the users entry. It is for a purchase history. I have this code, but it's not working. <?php $year = mysql_real_escape_string($_POST['year']); $month = mysql_real_escape_string($_POST['month']); $query3 = "SELECT * FROM affiliates1 WHERE purchase = '".date("m", strtotime($month))."' AND '".date("Y", strtotime($year))."'"; $w3 = mysql_query($query3) or die(mysql_error()); while ($b3 = mysql_fetch_array($w3)) { echo $b3['id']."<br />"; } ?> Can somebody show me the correct way? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/166008-solved-select-items-from-certain-date/ Share on other sites More sharing options...
PugJr Posted July 15, 2009 Share Posted July 15, 2009 I don't really understand. Are you wanting to explode 2009-01-30 into a month and year variable? Link to comment https://forums.phpfreaks.com/topic/166008-solved-select-items-from-certain-date/#findComment-875563 Share on other sites More sharing options...
timmah1 Posted July 15, 2009 Author Share Posted July 15, 2009 No. I have a table in the database formatted as date (Y-m-d) I have a form where a user can select a month and year to view their purchase history. I got it to work with this <?php $year = mysql_real_escape_string($_POST['year']); $month = mysql_real_escape_string($_POST['month']); $aff_id = mysql_real_escape_string($_POST['user']); $query3 = "SELECT * FROM affiliates1 WHERE DATE_FORMAT(`purchase`,'%Y-%m') = '$year-$month' AND affil_id = '$aff_id'"; $w3 = mysql_query($query3) or die(mysql_error()); while ($b3 = mysql_fetch_array($w3)) { echo $b3['id']."<br />"; } ?> Thanks for the response though Link to comment https://forums.phpfreaks.com/topic/166008-solved-select-items-from-certain-date/#findComment-875566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.