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
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.
Related
Assume that your webserver is detached from your database server. Their clocks might not be properly synchronized (down to milliseconds etc.).
You perform an insert such as "INSERT INTO sometable (VALUE,VALUE2,DATETIME) VALUES ("something","something else",NOW());"
Is there any way to get the timestamp generated by MySQL back into PHP? In the same fashion as when using $sqlObject->insert_id to get the AUTO_INCREMENT value of the last inserted row.
Edit:
I realize I could just use the insert_id and run a SELECT to get the timestamp, but this would require that I run another statement. I am wondering if it is possible in a single statement.
You can not get the direct value but you can do it via 2 ways.
1) Make a select query like select now() as cur_time. and take its value and use in the 2nd query. [ Its possible that you might get different time for this solution, SO i will suggest not to go with this solution and better you go with option 2. ]
2) After inserting the data get its ID and and make a query and get the inserted date time.
If your PHP and MYSQL server date time are same then you may use php's date function to get the current date / time .
I have a table with field name counselorDate which have value with date and time.But i want to get data between to dates only(not with time).I used following query.But its not worked. please help me
SELECT
*
FROM poolMainEnqDetails
WHERE counselorDate
LIKE BETWEEN ('2014-01-01%' AND '2014-01-03%')
The syntax of the BETWEEN is flawed, it should be
BETWEEN '2014-01-01%' AND '2014-01-03%'
Also, what does your sql management tool (PHPMyAdmin, SQL Workbench, etc...) tells you ? If it doesn't work, it should either give an error, either give a result you're not expecting.
If you compare a date against a datetime the that will be converted to datetime.
With that in mind you'll get the folowing where clause:
SELECT
*
FROM poolMainEnqDetails
WHERE counselorDate >= '2014-01-01'
AND counselorDate < '2014-01-04'
I have events in my MySQL database wich all have a date. When I perform a SQL query to get all the events in the future, I get an error... Although, the date of the events are in the future. When I change my SQL request to select dates in the past, I get those from the future...
The SQL statement below has worked before, but for some reason it stopped working...
I use this SQL request:
$sql = "SELECT * FROM calendar WHERE date >= CURDATE() order by `date`";
I get an empty array as result...
However if I change the query to this, I get all the events in my database:
$sql = "SELECT * FROM calendar WHERE date <= CURDATE() order by `date`";
This is my database data. In my opinion, all data are in the future...
The format of the date table is a default date-type:
When I ask my server about the time echo date("Y-m-d"); I get todays date as result...
So where do I make a mistake?
You may be checking the wrong date field. Do you have a created date as well as a scheduled date?
I could be crazy from the cold medicine I am on at the moment, but your date table can't possibly be the date of your calendar items, the id filed is only an int(2), that seems kind of small.
maybe something simplier? I notice the column name in your table is date, which also is the name of a function date() that returns the date part of a datetime value. If thats the case
$sql = "SELECT * FROM calendar c WHERE c.`date` <= CURDATE() order by `date`";
would do the trick. Even if not mysql itself, the gui app youre using (seems like phpmyadmin to me) might get confused.
(btw, you forgot the closing tick of date in the order by clause)
getting an empty set is meaning nothing is found matching. I would look at your formatting of your date. The only other thing i was thinking is that it is comparing an unmatched type so just returns an empty set.
use DATEDIFF :
DATEDIFF
WHERE DATEDIFF(date, CURDATE) > 0
Before you make your query, run this one:
SET time_zone = '-2:00'; // or whatever your time zone is.
Don't ask me how or why, but I've truncated my table and re-inserted some data and my query seems to work just fine:
$sql = "SELECT * FROM `calendar` WHERE `date` >= CURDATE() order by `date`";
So, despite the fact the problems seems to be solved by truncating my table, I would like to know the answer to the why-question... Anyone can provide me with it?
I am trying to compare two sets of times in order to find out if they're overlapping. Here is what I have at the moment..
$sql = "SELECT * FROM schedule WHERE starttime>='$starttime' AND endtime<='$endtime' AND day='$updateday'";
Now this doesn't work as it appears you cant compare time values...so I am completely unsure how this can be done?
Datetime fields in MySQL are stored as (for example)
'2011-05-03 17:01:00'
so you should be able to do something like
$starttime = date('Y-m-d H:i:s', $timestamp);
where $timestamp is a timestamp of the time you are concerned about. Then continue with your query.
You can make timestamps by using mktime() or strtotime() (if starting from a string representation of a time, like from an earlier MySQL query), or just time() for the current time.
I understand that you are using "time" for your datatype. This shouldn't be a problem, since you CAN compare fields using the "time" type. You might want to set your error reporting level to maximum or output your $sql statment just before mysql_query to doublecheck that you are constructing query which can return results at first place. Also check that you have valid dataset for your query in database (has happend to me once while debugging).
Why don't you use use unix timestamp to compare?
$sql = "SELECT * FROM schedule WHERE UNIX_TIMESTAMP(starttime)>=$starttime AND UNIX_TIMESTAMP(endtime)<=$endtime AND day='$updateday'";
Also I think you're comparing strings, which I'm not sure if it would work.
Assuming that you're using the TIME type, your format of "10:00:00" should work.
Not to sound like your mother, but be sure to parameterize your query after you get it working.
I did run the SELECT statement on the phpmyadmin on the sql before trying to do it on the webpage and was great, the thing is to use it and be thinking in this before:
CAST('the_value_you_want_to_be_compared', AS the_type_you_want_to_compare)
example:
If you want to compare the date of a day and your table name is activities:
SELECT * FROM activities WHERE date = CAST('2015-10-29', AS date)
and so on...
I once read in a performance blog that it is better to use PHP's date functions to set dates in a MySQL query instead of using mysql date functions like curdate() because mysql can then cache the query or the result or something like that. Does anyone have any insight into this? Does it hold any water or is it baseless?
example:
$query = 'SELECT id FROM table WHERE publish_date = \''.date('Y-m-d').'\'';
vs
$query = 'SELECT id FROM table WHERE publish_date = CURDATE()';
Any function containing CURDATE() will not be cached. Source
Hardcoding the date should still be cached as far as I can tell. Though you might want to consider using the prepare functionality instead of splicing strings into your query (for sanity and security sake).
It's quite simple actually. The MySQL server does not see your PHP code so it'll receive one of these:
SELECT id FROM table WHERE publish_date = '2010-01-18';
SELECT id FROM table WHERE publish_date = CURDATE();
It will not read your intentions either. For MySQL, '2010-01-18' is a string and is deterministic: its value is always '2010-01-18'. However, CURDATE() is not deterministic: its value varies depending on the date when you run it. Thus the first one is cacheable and the second one is not.
I personally preffer first way, because it give clear head about server time (time zone), my mysql server happend to be 10h earlier when promissed :)
localtime in your PHP script will apply in SQL