PHP MySQL hide rows more than 1 minute old - php

I am trying to make a PHP chat script and I want to make sure it doesn't display any entries older than 1 minute. The database contains a timestamp on each row called timestamp.
How can I do this?

SQL
SELECT *
FROM tablename
WHERE timestamp > TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE))
It's been a while since I've used that particular function, so I might be a bit off but it's close.
JavaScript
It sounds like you might want to actively hide old messages. In that case you need to include the timestamp meta-data so you can filter it using JavaScript.

Related

MySQL select where timenow > database time

I have a PHP script which creates a new order and inserts the time NOW + 15 minutes into the database.
After 15 minutes have expired the user cannot access the order page.
On the admin panel I only want to show records that have not expired, so my question is what select statement would I use to do this, and which would be the most efficent?
SELECT * FROM `orders` WHERE datenow > date
The "date" is the date of the order + 15 minutes
P.S.
Aternativly, is there any way to do this using PHP so I don't have to put too much stress on the database?
Many Thanks!
Given that you want records that have not yet expired, you want all records where date is greater than NOW().
SELECT
*
FROM
`orders`
WHERE
`date` > NOW()
As the comments mentioned, it is usually better to handle this on the database side; rather than transfer (assuming you have split database and web servers) the entire data set to filter it in the application code.
The other benefit of having all the datetime functions be on the same server (in the database in this case), is that especially on some shared hosting environments, the timezone for the database cannot be changed. Therefore, you want the time comparisons to occur in the same timezone.

how to make data base to delete info after some time using php?

I need to delete particular info from data base using only php, after some time without using cron system. How can I realize it?
Without cron there is only one way i.e hook that deletion code with some event e.g login of any user. As a new user logs in you can run that code
Include a timestamp in the database table. Then have a function in your PHP that deletes all records that are more than X minutes old whenever the PHP is run.
You can use a query such as this (for all records more than a day old).
DELETE FROM `table`
WHERE `timestamp` < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))

MySQL get data older than 30 minutes and perform some tasks

I am trying to make a coupon system, but I got stuck with something. I want to make a function that gets all data from coupon_session where the time (datetime) is older than 30 minutes. For each of the results, I want to fetch the "code" from the row, and after that I want it to delete the row.
I've already figured out how to retrieve the data, but the rest is kinda hard for me. Could you guys help a bit?
This is my code for fetching all > 30 min old rows:
mysql_query("GET * FROM `coupon_session` WHERE TIMESTAMPDIFF(MINUTE,time,NOW()) > 30");
The code to fetch what you want is:
select cs.code
from coupon_session cs
where time < date_sub(now(), interval 30 minute);
Deleting is another problem, because the value of now() changes. You have several choices, such as:
Get the list of ids to delete in your application and delete those (probably the safest method).
Calculate "now" in your application and use the same value for the select and delete (also safe).
Calculate the maximum time returned by the select and use that for the subsequent delete.

PHP set interval of a process

I have an API fetcher script that reads values from XML files and updates data to mysql. It updates the database everytime it's called via ajax.
Now I want to set an interval for about a day before the query gets updated. Which php function or method should I use?
I think I should make myself a little more clear, these are the steps:
xml file read with php
Echo the results.
--->If (mysql updated time > 24*60*60 seconds) update the db
--->if not, do nothing.
I can't figure out how should I process the step (3)
Well! as far as i understood your question. I think you should do with SQL not with PHP.
Save update time in db with query of Insert. And call a stored procedure every time you update. like in MySQL:
Begin
//Adding updtime column value into a Variable called #a
select #a:=updtime from abc order by id desc limit 1;
//DATE_ADD() is a function of MySQL to get Time after adding Hours or Minutes etc. More can be found in MySQL Manual online
if DATE_ADD(#a, INTERVAL 24 HOUR) > now()
then insert into abc values ('XML',now());
end if;
End
You can also do it without a variable and that would be much better as
if DATE_ADD((select updtime from abc order by id desc limit 1), INTERVAL 24 HOUR) > now()
The best solution that I can think of is a Cron Job.
This page will give you detailed information

Letting MySQL figure out date intervals vs. PHP

In a MySQL 5.1 InnoDB environment, what is the best way to SELECT data based on date intervals?
Letting MySQL do it via something like DATE_SUB(CURDATE(), INTERVAL 5 DAY) > created_at
Or have PHP prepare the date before submission via strtotime?
I would do it via the query to MySql. That way, you keep the logic of selecting dates out of the PHP. The PHP just handles the display, and you get the advantage of smaller chunks of data coming out of the database as well.
I don't think it matters from performance point of view in this case. Your expression (DATE_SUB) will be evaluated just once. Another point is that your webserver and mysql server can use different timezones, so you may have different results.
I would keep as much logic closest to the database - it has more ability to do any optimisations. Also gives the ability to do change tables easier, change the use of PHP (perhaps java/C# ...) in the future. It also differentiates beteen database (for the data), PHP (for HTML delivery), Javascript (for user enjoyment), CSS (to make things pretty)
You can select a date range using BETWEEN ... AND ...
SELECT * FROM table1 t1 WHERE now() BETWEEN t1.startdate AND t1.enddate
or
SELECT * FROM table1 t1
WHERE t1.somedate BETWEEN DATE_SUB(now(),INTERVAL 1 DAY) AND now()
I prefer this syntax because it's so close to my mental picture of ranges.

Categories