patheticsam Posted August 8, 2012 Share Posted August 8, 2012 Hi, I'm a little bit new to php and I'm working on a script to display upcoming events. Here's the script I have : <?php require_once('../admin/config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $date = Date("Y-m-d"); $data = mysql_query("SELECT * FROM events WHERE event_date > $date ORDER by event_date") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { ?> <div class="indent-bottom17 border-bottom p8"> <?php echo $date; ?> <h3 class="p4-1"><?php echo $info['event_city'].", ".$info['event_state']; ?></h3> <h6 class="p4-1">Course title : <?php echo $info['event_title']; ?></h6> <a href="view_event.php?cmd=view&id=<?php echo $info['event_id']; ?>">View details...</a> </div> <?php } ?> I don't get any errors and the script seems to work except for the part that it also displays events from the past...it ignores the condition : SELECT * FROM events WHERE event_date > $date ORDER by event_date I don't know if i'm doing this right but if anyone can point me to a solutions it would be really appreciated...Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/266819-problem-with-date-ans-select-please-help/ Share on other sites More sharing options...
patheticsam Posted August 8, 2012 Author Share Posted August 8, 2012 p.s. the event_date field in MySQL is correctly set to DATE type... Quote Link to comment https://forums.phpfreaks.com/topic/266819-problem-with-date-ans-select-please-help/#findComment-1367849 Share on other sites More sharing options...
PFMaBiSmAd Posted August 8, 2012 Share Posted August 8, 2012 Literal dates in a query are strings and need to be enclosed by single-quotes WHERE event_date > '$date' Without the single-quotes, a date looks like a math subtraction problem: WHERE event_date > 2012-08-08 evaluates to WHERE event_date > 1996 Quote Link to comment https://forums.phpfreaks.com/topic/266819-problem-with-date-ans-select-please-help/#findComment-1367850 Share on other sites More sharing options...
patheticsam Posted August 8, 2012 Author Share Posted August 8, 2012 Works,...I knew it was something small. Thanks! Really appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/266819-problem-with-date-ans-select-please-help/#findComment-1367851 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.