How to get time since MySQL database last edited in PHP - php

I need to know the time since a table in my MySQL database was edited. Is there any way to do this in PHP? The only way I can think of is to get the update time, and compare it to the current time (which will be a little bothersome).

SELECT TIMEDIFF(CURRENT_TIME, UPDATE_TIME)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'my_table'

One solution would be to add a timestamp field, that is automatically updated whenever a row is changed.
Then you can find the last change time by selecting the last update value:
# Add a timestamp column:
ALTER TABLE [TABLENAME] add column `ts_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP;
# Get the last update value:
SELECT MAX(ts_update) from [TABLENAME];

It's a bit of a pain since SHOW TABLE STATUS doesn't seem to return a standard result set. Every way I tried to use it as a subquery failed. It seems like you'll need to figure it out programmatically.
SHOW TABLE STATUS
WHERE name = 'target_table';
And if you're concerned about the timezone just do a separate SELECT NOW() to get the time the mysql server has.
Note: This will only show you the time at which the table schema was updated. If you want to know when the last time a row was inserted/edited you'll have to add a timestamp column like Amirshk suggested.

Related

How to insert data dynamically in MySQL

I need to record the date each event happens. I have this table.
Click here to view the table
NULL spaces are available to save a new date.
The X represents the date that the event occurred.
The problem is I do not know how to update the date each event occurs
I need to know the best option if you use INSERT or UPDATE querys.
Thank you for your help
If you just want to update a column in a table with the current time:
UPDATE `yourtable` SET `yourcolumn` = NOW() WHERE `ID` = yourid
Assuming your columns are DATETIME() columns.
As an aside, it's best to have event-related information in another relationship table. This way you can link multiple events for each row in your main table. This provides a more accurate data-trail for accounting purposes (or in other words, you can see each and every update without overwriting anything).
If you are seeking to have a 'last_modified' column on your table to help you keep track of changes made on your records, you should do this:
ALTER TABLE my_table
ADD last_modified TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
Basically what it does is to add a 'last_modified' column, Set it to be a timestamp and make it to be updated with the current timestamp when there is an update on the record.

MySQL insert date if no field exists

Is there a way in MySQL to get insert date/time for rows if there was no insert_date field. I have a database which I configured to store insert_date but can I populate the field before that change ( month ago ). Is that even possible?
Pull that insert date/time from log or something else?
Nope. If that date wasn't stored before, it's impossible to find out when a row was inserted. The best you can do is just pick a date, or maybe make a rough estimate if you have information to base that on (for instance, the create date of a customer might be related to the date of their first invoice)..
By the way, you can add timestamp columns and specify the clause DEFAULT CURRENT_TIMESTAMP to set a timestamp as soon as you insert the row. That way, you don't need a trigger to update the row.
You can even add a clause ON UPDATE CURRENT_TIMESTAMP so the column (or a different column) is updated automatically too.
See Timestamp initialization for more information about this subject.
This doesn't change the fact, though, that you cannot get those values for rows that already exist.

add identical data to one column for ALL rows (mySQL) via phpMyAdmin

I am creating my first (!) database, and I have run into an issue that I cannot seem to locate the answer for.
I have put an "added on" field in a table (among other things ofc), and since I'm the one adding it, I want to put the same date in the entire column. The idea is that if there is a new item added at a later date, it will have that date, but the data initially populated should all have the same date.
How? Please don't tell me one row at a time....
Just add the column to the table and then run an update query
update yourtable set nameofyournewfield = 'yourdate'
This will update all rows currently in the db, while the new rows will gettheir value (or have the default value you provided)
Another possibility in addition to #Nicola's answer is to use the DEFAULT argument in add column.
You can set default property of that column to any date and it will replicate that for all rows as long as you don't specify any value for that column while inserting and when you want to insert a different value just specify it in the insert statement.

What's the fastest way to poll a MySQL table for new rows?

My application needs to poll a MySQL database for new rows. Every time new rows are added, they should be retrieved. I was thinking of creating a trigger to place references to new rows on a separate table. The original table has over 300,000 rows.
The application is built in PHP.
Some good answers, i think the question deserves a bounty.
For external applications I find using a TimeStamp column is a more robust method that is independent of auto id and other primary key issues
Add columns to the tables such as:
insertedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP
or to track inserts and updates
updatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
In the external application all you need to do is track the last timestamp when you did a poll. Then select from that timestamp forward on all the relevant tables. In large tables you may need to index the timestamp column
You can use the following statement to find out if a new record was inserted in the table:
select max(id) from table_name
replacing the name of primary key and table name in the above statement. Keep the max(id) value in a temporary variable, and retrieve all new records between this and the last saved max(id) value. After fetching the new records, set max(id) value to the one you got from the query.
Create a PHP Daemon to monitor the MySQL Table File size, if size changes query for new records, if new records found run next process.
I think there is an active PEAR daemon you can easily configure to monitor the MySQL Table file size and kick off your script.
assuming you have an identify or some other data that always grow, you should keep track on your php application of the last id retrieved.
that'd work for most scenarios. Unless you are into the real time camp, I don't think you'd need any more than that.
I would do something like this. Of course, this is assuming that ID is an incrementing numerical ID.
And how you store your "current location" in the database is upto you.
<?
$idFile = 'lastID.dat';
if(is_file($idFile)){
$lastSelectedId = (int)file_get_contents($idFile);
} else {
$lastSelectedId = 0;
}
$res = mysql_query("select * from table_name where id > {$lastSelectedId}");
while($row = mysql_fetch_assoc($res)){
// Do something with the new rows
if($row['id']>$lastSelectedId){
$lastSelectedId = $row['id'];
}
}
file_put_contents($idFile,$lastSelectedId);
?>
I would concurr with TFD's answer about keeping track of a timestamp in an separate file/table and then fetching all rows newer than that. That's how I do it for a similar application.
Your application querying a single row table (or file) to see if a timestamp has changed from the local storage should not be much of a performance hit. Then, fetching new rows from the 300k row table based on timestamp should again be fine, assuming timestamp is properly indexed.
However, reading your question I was curious if Mysql triggers can do system calls, say a php script that would do some heavy lifting. Turns out they can by using the sys_exec() User-Defined Function. You could use this to do all sorts of processing by passing into it the inserted row data, essentially having an instant notification of inserts.
Finally, a word of caution about using triggers to call external applications.
One option might be to use an INSERT INTO SELECT statement. Taking from the suggestions using timestamps to pull the latest rows, you could do something like...
INSERT INTO t2 (
SELECT *
FROM t1
WHERE createdts > DATE_SUB(NOW(), INTERVAL 1 HOUR)
);
This would take all of the rows inserted in the previous hour and insert them in to table 2. You could have a script run this query and have it run every hour (or whatever interval you need).
This would drastically simplify your PHP script for pulling rows as you wouldn't need to iterate over any rows. It also gets rid of having to keep track of the last insert id.
The solution Fanis purposed also sounds like it could be interesting as well.
As a note, the select query in the above insert can but adjusted to only insert certain fields. If you only need certain fields, you would need to specify them in the insert like so...
INSERT INTO t2 (field1, field2) (
SELECT field1, field2
FROM t1
WHERE createdts > DATE_SUB(NOW(), INTERVAL 1 HOUR)
);

How to add date with PHP/MySQL?

I want to automatically add date in order_date field when a customer checkouts my online shop.
What is the best way to do it?
There are other fields like, delivery data and payment date in the same table.
Is it good idea to add a hidden field so that when a cutomer submit, the date will be added?
But I am not sure how to do it.
Can anyone suggests me the better way to do it please?
No, you don't need a hidden form field. You can do this directly in MySQL.
Assuming that your order_date field is a DATE. DATETIME or TIMESTAMP, then in your SQL that inserts the order record, simply put NOW() as the value for order_date:
INSERT INTO orders (x,y,z,order_date) VALUES ('x','y','z',NOW());
In the update statement that finalizes your order record, do something like this:
update order set ....., order_date = now() where ...
You don't want to rely on client-side date anyway. It could be off, and think of the timezones. You want a single source of dates in your system, and that should be either the database or the server-side code layer (PHP or what have you).
When a customer places an order you can use a TIMESTAMP column to track when the order was placed / the order was saved the database. Something like:
ALTER TABLE sales_order ADD COLUMN date_placed TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
The DEFAULT CURRENT_TIMESTAMP will be filled with the current time when the row is created, but not on subsequent updates. See the docs for more:
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
With a DEFAULT CURRENT_TIMESTAMP
clause and no ON UPDATE clause, the
column has the current timestamp for
its default value but is not
automatically updated.

Categories