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

ozonostudio
  • 119
  • 2
  • 6
  • 13
  • 1
    You should be able to use `between`. `select * from guide where now() between start and end`. – chris85 Jan 13 '16 at 02:49
  • I didn't understand what you want. It should be "value between **two** hours" – Vinícius Fagundes Jan 13 '16 at 02: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 – ozonostudio Jan 13 '16 at 02: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` – ozonostudio Jan 13 '16 at 03:03

1 Answers1

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.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • 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? – ozonostudio Jan 13 '16 at 03:10
  • 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). – chris85 Jan 13 '16 at 03:13
  • Thanks for you quick and really nice solution – ozonostudio Jan 13 '16 at 03:16