How can I tell when a MySQL table was last updated?

后端 未结 16 1841
忘了有多久
忘了有多久 2020-11-22 17:17

In the footer of my page, I would like to add something like \"last updated the xx/xx/200x\" with this date being the last time a certain mySQL table has been updated.

16条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 17:41

    In later versions of MySQL you can use the information_schema database to tell you when another table was updated:

    SELECT UPDATE_TIME
    FROM   information_schema.tables
    WHERE  TABLE_SCHEMA = 'dbname'
       AND TABLE_NAME = 'tabname'
    

    This does of course mean opening a connection to the database.


    An alternative option would be to "touch" a particular file whenever the MySQL table is updated:

    On database updates:

    • Open your timestamp file in O_RDRW mode
    • close it again

    or alternatively

    • use touch(), the PHP equivalent of the utimes() function, to change the file timestamp.

    On page display:

    • use stat() to read back the file modification time.

提交回复
热议问题