0

I'm trying to get a specific show description from my DB but I really don't know how, I know i'm new into this, the table (guide) have 2 DATETIME values "start" and "end"

If I use this:

$sql = mysql_query("SELECT * FROM guide WHERE start  >= CURDATE()");

Only return the first value from the table, and inclusive its the wrong value, please I need some help to get this, because I don't find a solution from other on this web

4
  • 1
    You should be able to use between. select * from guide where now() between start and end. Commented Jan 13, 2016 at 2:49
  • I didn't understand what you want. It should be "value between two hours" Commented Jan 13, 2016 at 2:50
  • I'm trying to create some kind of TV Guide and I want to show the current show on air, but the shows have an "start hour" and a "end hour", and the only value that I have to get this is the current time like this: 2016-01-12 20:45:14 so my problem is how can I get the current show using that Commented Jan 13, 2016 at 2:57
  • @chris85 your solution works fine, now how I use a 3rd value for get the specific show? I mean the "channel" row, something like: where channel = $channel_ID Commented Jan 13, 2016 at 3:03

1 Answer 1

1

You should be able to use mysql's between function to pull the records in the current time range.

select * from guide where now() between start and end

To limit the returns you can add in additional parameters, this may give you back no results though so have a default value.

select * from guide where channel = $channel_ID and now() between start and end

You also should look into parameterized queries and updating your driver. Having variables in your query isn't the best practice.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks to your first solution I ended using: $sql = mysql_query("SELECT * FROM guide WHERE (NOW() BETWEEN start AND end) AND channel = 13"); And works, this is OK or may cause any issue?
That query should be fine, depending on how your application runs it may display things strangely when no results are found/available though (or maybe that won't happen).
Thanks for you quick and really nice solution

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.