views:

28

answers:

2

Table in MySQL have 1 problem column: creation_date.

During inserting a new row through PHP, I thought that there would be correct to insert the date directly in the query, MySQL has to do it himself.

Do I need to do the trigger, or it would be better to use PHP for this, as intended? How would you have done to?

PS: http://stackoverflow.com/questions/2906978/mysql-how-to-create-trigger-for-setting-creation-date-for-new-rows

+3  A: 

Use TIMESTAMP DEFAULT CURRENT_TIMESTAMP field

http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

Mchl
Take care with this, as the timestamp column will get set on an UPDATE. Your updates will have to specify TIMESTAMP_COLUMN = TIMESTAMP_COLUMN as one of the things SET, otherwise the creation date will be lost.
Brian Hooper
Only if it will have ON UPDATE CURRENT_TIMESTAMP in CREATE TABLE definition.
Mchl
It does't work. On CREATE do work, but on UPDATE it set to NULL.
HWTech
+1  A: 

If you create a trigger for your table (or adopt the suggestion above) you won't have to remember to do it in your PHP. This advantage will show when someone else creates another PHP to insert into the same table.

Brian Hooper