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?
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.
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.
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.
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'.
What am I missing?
You don't update :)
Use DEFAULT CURRENT_TIMESTAMP
along with ON UPDATE CURRENT_TIMESTAMP
Try setting the default value to CURRENT_TIMESTAMP
instead of putting that in the attributes.
MySQL Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With