После нескольких часов отладки моего PHP-кода, поскольку даты сохранялись неправильно, я понял, что XAMPP
автоматически добавлял атрибут DEFAULT
и атрибут ON UPDATE
в мой столбец date_added
.
У меня есть эта таблица:
CREATE TABLE test_table(
date_added timestamp
NOT NULL,
last_updated timestamp
NOT NULL
);
Как видите, я явно не устанавливаю значение DEFAULT
или тег ON UPDATE
в своем коде создания таблицы. Однако, если я зайду в phpmyadmin и посмотрю на структуру этой таблицы, она будет выглядеть так:
Я попытался нажать «изменить» и установить DEFAULT
на None
и ON UPDATE
на пустое, но это не меняет его. Любые предложения о том, как я могу удалить эти вещи?
Это связано с настройкой системной переменной explicit_defaults_for_timestamp
, которая в MySQL < 8.0.2 по умолчанию равна OFF
. Когда это OFF
,
If explicit_defaults_for_timestamp is disabled, the server enables the nonstandard behaviors and handles TIMESTAMP columns as follows:
TIMESTAMP columns not explicitly declared with the NULL attribute are automatically declared with the NOT NULL attribute. Assigning such a column a value of NULL is permitted and sets the column to the current timestamp.
The first TIMESTAMP column in a table, if not explicitly declared with the NULL attribute or an explicit DEFAULT or ON UPDATE attribute, is automatically declared with the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.
TIMESTAMP columns following the first one, if not explicitly declared with the NULL attribute or an explicit DEFAULT attribute, are automatically declared as DEFAULT '0000-00-00 00:00:00' (the “zero” timestamp). For inserted rows that specify no explicit value for such a column, the column is assigned '0000-00-00 00:00:00' and no warning occurs.
Depending on whether strict SQL mode or the NO_ZERO_DATE SQL mode is enabled, a default value of '0000-00-00 00:00:00' may be invalid. Be aware that the TRADITIONAL SQL mode includes strict mode and NO_ZERO_DATE. See Section 5.1.11, “Server SQL Modes”.
См. руководство.
Этот вопрос обсуждает, как изменить настройку.
Вы можете попробовать отправить запрос, который устанавливает переменную при установлении соединения.
Как мне это сделать?
SET GLOBAL explicit_defaults_for_timestamp = 1
. Если вы не можете этого сделать, вам нужно будет отредактировать файл конфигурации MySQL, как описано в вопросе, который я связал.
есть ли способ установить это с помощью php (когда я делаю соединение mysqli)