MySQL 插入 datetime = NOW() 不起作用?
我有以下代码(php、mysql、pdo):
$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = NOW()");
$stmt->execute(array($party));
运行时,该方已正确插入,但日期未按应有的方式插入(系统日期和时间)。我已经多次验证日期的字段类型是日期时间。
有什么想法吗?
编辑
要给出实际数据和返回的结果:
假设以下内容:
$party = 'John';
结果返回:
party | date
-------------------------------------
John | 0000-00-00 00:00:00
更新:
当我直接在 mysql 查询浏览器中运行以下代码时,插入工作正常:
insert into agent_temp set party = 'John', date = NOW();
返回:
party | date
-------------------------------------
John | 2010-12-28 13:15:23
已回答
那么,谁准备好杀我了?我不知道是什么引起了这个问题,但不幸的是,这个问题似乎是由于我的机器上的早期版本的 php 脚本被缓存并且仍在运行错误数据。我刷新、关闭并清空浏览器,现在脚本可以运行了。我很抱歉让每个人的大脑都融化了一点。
I have the following code (php, mysql, pdo):
$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = NOW()");
$stmt->execute(array($party));
when run, the party is insert correctly but the date is not inserting as it should (the system date and time at action). I have verified numerous times the field type for date is datetime.
Any ideas?
EDIT
To give actual data and the results returned:
assume the following:
$party = 'John';
the results return:
party | date
-------------------------------------
John | 0000-00-00 00:00:00
update:
When i run the following code directly within a mysql query browser, the insert works just as it should:
insert into agent_temp set party = 'John', date = NOW();
returning:
party | date
-------------------------------------
John | 2010-12-28 13:15:23
ANSWERED
Well, who is ready to kill me? I have no idea what caught it up but unfortunately the issue seemingly was due to an earlier version of the php script from my machine being cached and still running bad data. I refreshed, closed, and emptied my browser and now the script works. My apologies for making everybody's brains melt just a little.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
怎么样:
How about:
我不确定您使用的是哪个系统,但我认为您的准备语句将在
NOW()
部分周围添加引号 - 导致语句尝试插入 NOW()< /em> 而不是运行 mysql 函数NOW()
- 因此,因为该字段无法存储字符 NOW() 你得到 000-00.... 出于兴趣,尝试将字段类型从
DATETIME
更改为TEXT
并查看运行命令时会得到什么。I'm not sure which system you are using, but I would think your prepare statement will be adding quotation around the
NOW()
part - causing the statement to try and insert NOW() instead of running the mysql functionNOW()
- thus because the field can't store the characters NOW() you get the 000-00.....Out of interest, try changing the field type from
DATETIME
toTEXT
and see what you get when you run the command.这是一个纯粹的 INSERT 语句,它不会更新任何行。
This is a pure
INSERT
statement, it does not update any rows.未经测试,但问题可能是“日期”是保留字,您可以尝试重命名您的列,看看它是否有效。
另一种方法是添加带有“ON UPDATE CURRENT_TIMESTAMP”的时间戳字段。
Untested but perhaps the problem is that 'date' is a reserved word, you could try renaming your column and see if it works.
The other approach is to add a timestamp-field with an 'ON UPDATE CURRENT_TIMESTAMP'.