Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:20:54+00:00 2026-06-04T23:20:54+00:00

Is it possible to use an if statement in a MySQL query in a

  • 0

Is it possible to use an if statement in a MySQL query in a similar what I’m showing? I want to grab some information for upcoming events(not ones with previous dates).

ini_set('date.timezone', 'America/New_York');
    echo $timestamp = date('Y-m-d'); 
    $sql = "select eventID,eventTitle,eventDate from events where eventLocationID = $locationID AND (eventDate > $timestamp) ORDER BY eventDate ASC";

edit – the sql statement isn’t filtering out dates prior to today (it’s displaying everything).

For example, I’m echoing the timestamp just to be sure it’s setting properly. In my table I entered two dates, one in january and one today.

So here is the output

  2012-05-31 //timestamp, not part of results. just for reference
  2012-01-10 //first result, however it shouldn't display
  2012-06-22 //this should be the only result that displays
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:20:56+00:00Added an answer on June 4, 2026 at 11:20 pm

    Just use a where clause:

    $date = date('Y-m-d');
    $sql = "
        SELECT 
            foo 
        FROM 
            bar 
        WHERE
            eventLocationId = ... 
            AND eventDate > '$date' 
        ORDER BY 
            eventDate DESC
    ";
    

    Also, m/d/Y is not a valid date format for MySQL. It expects Y-m-d.


    On a note not directly related to your question:

    I really want to avoid doing post-query if statements

    If you filter a resultset in PHP, it’s going to many times slower than if you use a where clause. Databases are made to filter records quickly and efficiently. And, filtering in PHP requires you to actually transfer all of the results from MySQL to PHP (another slow down).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible use a MySQL query to perform this kind of check? If
Is it possible to use SELECT statement in MySQL such that key of the
Is it possible in mysql to use IN statement with varchar column In my
Is it possible to use AS sql statement with Django ORM: SELECT my_field AS
I'm not even sure if this is quite possible to do with MySQL, but
Is it possible to use $_SESSION[var] in INSERT and SELECT statements of mysql as
Is it possible to write an SQL statement for MySQL which finds all rows
is there an if statement when it comes to mysql query statements? when i
In MySQL, it is possible to use an alias in GROUP BY clause that
Possible Duplicate: Performing simple arithmetic in MySQL statement or in PHP code Im wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.