MySQL Timestamp - why all zeros?

和自甴很熟 提交于 2020-01-22 12:38:36

问题


I'm using PHPMyAdmin and I've got a MySQL table column called "timestamp." The type (surprise!) is TIMESTAMP, and in 'attributes' I've set it to ON UPDATE CURRENT_TIMESTAMP.

However, each new record gets a timestamp that looks like this:

0000-00-00 00:00:00

I have explicitly set the default value to none, but when I save and come back to look, it is set to all zeros as above.

The relevant PHP records page hits with this query:

$query = "INSERT INTO `pagehit` (user_id, pageurl)
VALUES ('" . $userid . "', '" . $pageurl . "')";

The whole thing is running under XAMPP.

What am I missing?


回答1:


What am I missing?

You don't update :)

Use DEFAULT CURRENT_TIMESTAMP along with ON UPDATE CURRENT_TIMESTAMP




回答2:


Try setting the default value to CURRENT_TIMESTAMP instead of putting that in the attributes.

MySQL Reference




回答3:


If your timestamp column captures only the insertion time then use only

timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Otherwise if it is for modification time then use like as follows

timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP



回答4:


In my case works like this:

In phpMyAdmin:

ALTER TABLE `name_table`  ADD  `name_row` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

In PHP sintax for the row:

date('Y-m-d H:i:s')


来源:https://stackoverflow.com/questions/837190/mysql-timestamp-why-all-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!