简体   繁体   English

奇怪的 MYSQL/PHP 错误:insert with now()+$i as timestamp occasionally inserts 0

[英]Strange MYSQL/PHP error: inserts with now()+$i as timestamp occasionally inserts 0

I observed this strange problem.我观察到这个奇怪的问题。 Inserting with now() is fine, but when adding a number to now() it sometimes inserts 0. It seems random.now()插入很好,但是当向now()添加数字时,它有时会插入 0。这看起来是随机的。

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表: ENGINE=InnoDB AUTO_INCREMENT=164865 DEFAULT CHARSET=latin1

mysql_version mysql_版本

+-------------------------+------------------------------------------+
| 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.我在一个循环中运行了 100 次,其中大约前 1/3ish 插入时间为 0,其中 rest 没问题。 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 return 20120409161530.000000 .在这里猜测......但它似乎在数字上下文中(比如,如果你将一个数字添加到返回值), NOW()返回一个看起来像时间戳的数字(而不是秒数。),就像,今天它可能返回20120409161530.000000 Now...depending on the value of $i , it seems this could easily make for an invalid value.现在……根据$i的值,这似乎很容易导致无效值。 I mean, what if it's 666666 ?我的意思是,如果它是666666怎么办? You'd end up with 20120409828196.000000 , which makes no sense as a timestamp...and i'm pretty sure that MySQL just turns invalid values to 0.你最终会得到20120409828196.000000 ,这作为时间戳没有任何意义......而且我很确定 MySQL 只是将无效值变为 0。

You may want to consider something like NOW() + INTERVAL $i SECOND , and redefine $i to be the number of seconds (or minutes, days, even years if you like...just change SECOND to whatever you decide on).您可能需要考虑像NOW() + INTERVAL $i SECOND这样的东西,并将$i重新定义为秒数(或者分钟、天、甚至年,如果您愿意的话……只需将SECOND更改为您决定的任何值)。 Your resulting times are less likely to turn out all messed up.您得到的时间不太可能全部搞砸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM