[Tutorial] Storing player chat log in MySQL, automatic deletion and periodic backups
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
Use of the datetime field is up for discussion. I've always preferred regular Unix timestamps. In any case you should use UTC_TIMESTAMP() rather than NOW(). The latter one gets the local server time, which can lead to inconsistencies if migrating the server to another country (e.g. from Europe to the US) or even if trying to display data in the user's local time.
There doesn't appear to any specific performance or flexibility benefit of using unix timestamp, so I personally prefer MySQL datetime, and would recommend you to use it as well. You can also use MySQL's native date and time functions, and in addition it's human readable without any conversion. The latter point is not that important, but at least I find the date and time functions very powerful and easy to use.

What I do agree on is using UTC_TIMESTAMP() or UTC_DATE() rather than NOW(), as, like you said, it guarantees the there will be no issues with the timezones.

Further reading: http://stackoverflow.com/questions/4...e-vs-timestamp


Edit: On topic:
It might be worth mentioning that the input should be escaped.

Код:
format(pQuery, sizeof(pQuery), "INSERT INTO "#TABLE_NAME"(Username, Time, Text) VALUES('%s', NOW(), '%s')", pName, text);
If someone were to say
Код:
'); TRUNCATE TABLE `chat`; --
you'd likely to lose your data, assuming they could guess the name of the table. Or even worse, if the mysql user has access to more tables/database, they can as well be potentionally deleted/modified by trying some of common table/database names.

Another thing: You should probably use datetime field instead of date, as it would most likely be useful to store the time of the message as well, not just the date.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)