Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Timestamp - why all zeros?

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?

like image 422
Nathan Long Avatar asked May 07 '09 21:05

Nathan Long


People also ask

What is the format of TIMESTAMP in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

Can TIMESTAMP be null in MySQL?

If the explicit_defaults_for_timestamp system variable is disabled, TIMESTAMP columns by default are NOT NULL , cannot contain NULL values, and assigning NULL assigns the current timestamp. To permit a TIMESTAMP column to contain NULL , explicitly declare it with the NULL attribute.

How is TIMESTAMP stored in MySQL?

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME, which is stored “as is”.) By default, the current time zone for each connection is the server's time.

How do I add a default value to a TIMESTAMP column?

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example, DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.


2 Answers

What am I missing?

You don't update :)

Use DEFAULT CURRENT_TIMESTAMP along with ON UPDATE CURRENT_TIMESTAMP

like image 160
Quassnoi Avatar answered Oct 23 '22 04:10

Quassnoi


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

MySQL Reference

like image 31
Kekoa Avatar answered Oct 23 '22 04:10

Kekoa