I observed this strange problem. Inserting with now() is fine, but when adding a number to now() it sometimes inserts 0. It seems random.
This is the query
mysql_query("INSERT INTO action_data (user_id, value, create_time, site_id) VALUES (807809, 20, now()+$i, 409666)");
the table: ENGINE=InnoDB AUTO_INCREMENT=164865 DEFAULT CHARSET=latin1
mysql_version
+-------------------------+------------------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------------------+
| innodb_version | 1.0.13-11.6 |
| protocol_version | 10 |
| version | 5.1.52-rel11.6-log |
| version_comment | Percona Server (GPL), 11.6, Revision 140 |
| version_compile_machine | x86_64 |
| version_compile_os | unknown-linux-gnu |
+-------------------------+------------------------------------------+
I ran it 100 times in a loop and about first 1/3ish of them are inserted with time 0 and rest of them are fine. Anybody have an idea why it’s happening?
Guessing here…but it seems in a numeric context (like, say, if you add a number to the return value),
NOW()returns a timestampish-looking number (not a count of seconds!). Like, today it might return20120409161530.000000. Now…depending on the value of$i, it seems this could easily make for an invalid value. I mean, what if it’s666666? You’d end up with20120409828196.000000, which makes no sense as a timestamp…and i’m pretty sure that MySQL just turns invalid values to 0.You may want to consider something like
NOW() + INTERVAL $i SECOND, and redefine$ito be the number of seconds (or minutes, days, even years if you like…just changeSECONDto whatever you decide on). Your resulting times are less likely to turn out all messed up.