-1


I am working on a MySQL query where display only upcoming events. But I got a blank screen every time and displaying in the past event list.

Today = 22 July 2018,
Upcoming event date (eventID) = 29 July 2018

My query is:

SELECT * FROM event WHERE eventDate > CURDATE() ORDER BY STR_TO_DATE(eventDate,'%d %F %Y')

PHP Code (without css):

$result = mysqli_query($con, $sql);


                                    while($row = mysqli_fetch_assoc($result)) { ?>

<?php echo $row['eventName'] . "<br/>" ?>
<?php echo $row["eventDate"] . "<br/><br/>" ?>
<?php } ?>

Database Table:

eventID int(11) AUTO_INCREMENT
eventName varchar(255)
eventDate varchar(255)

I already tried with CURRENT_DATE() , NOW() & >=

As I told my upcoming event shows in the past list, with all other events. eventDate < CURDATE()

13
  • 1
    Your SQL suggests event date is a string rather than a date column. What are your error logs? What is your PHP? Commented Jul 22, 2018 at 18:30
  • Also Show your table Commented Jul 22, 2018 at 18:37
  • 1
    voting this as a typo, Martin's right about your most likely storing as anything other than a DATE type which you should and will help you a lot and won't make you tear out your hair, as it were. Commented Jul 22, 2018 at 18:39
  • 1
    @FunkFortyNiner a recent study of developers found that those who work with MySQL / PHP have a 23% more likely chance of being bald. I think you just found a cause! Commented Jul 22, 2018 at 18:41
  • @Martin Please see my php code. Commented Jul 22, 2018 at 18:41

1 Answer 1

1

Your problem is that you're comparing a DATE with a STRING . Results will be unpredictable, at best. You need to rewrite your table structure to set the eventDate as a DATE rather than a VARCHAR column.

eventDate varchar(255)  <== varchar is the WRONG type here; you want DATETIME or DATE or similar. 

Please READ HERE about how to convert your columns correctly to DATE type.

I have rewritten your code for you, because I feel that this place should take some tips from CodeReview.

$sql = "SELECT eventName, eventDate FROM event WHERE eventDate > CURDATE() ORDER BY eventDate";

$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result)) { 
    echo htmlentities($row['eventName']) . "<br/>"
    echo $row["eventDate"] . "<br/><br/>"
 } 
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.