mysql行时间戳,创建新行条目时从mysql行检索时间戳

博客讨论了在MySQL中插入数据时如何同时获取自动生成的时间戳。建议在PHP中存储当前时间戳并在插入时使用,以确保一致性。如果需要时间戳作为唯一标识,这种方法更有效,避免了两次查询。同时也提到,在某些情况下,直接在MySQL中自动填充时间戳字段也是合理的。
摘要由CSDN通过智能技术生成

Is this valid?

$timestamp = mysql_query("INSERT INTO column1 (entry1, entry2)

VALUES ('$entry1', '$entry2')

AND SELECT timestamp");

?>

timestamp is a mysql column which generates a timestamp upon row creation.

e.g.

-do I need to specify creation of "timestamp" row? (I think it is okay, configured to do it on its own)

-can these two actions be executed within the same query? if not, how can I do it? I want to make sure that I get the timestamp, as I use it later in the code also as a unique identifier as this entry. how can I otherwise get it? (assuming there may be entries which are multiple and similar in many of the other row entries).

Thanks!

解决方案

I depends on what you need the info for. If you need it to be stored against other rows/entities and you want it to be consistent then I would store the date in PHP (possible once per request in a web app) and then use that throughout, e.g.

$currentTimestamp = time();

$timestamp = mysql_query("INSERT INTO column1 (entry1, entry2, timestamp_col)

VALUES ('$entry1', '$entry2', $currentTimestamp);

That way everywhere you use the timestamp it will be consistent. Your request doesn't have to take seconds for the timestamp to vary within it.

Even if you're not storing it against anything else you still might want to use this method if you need to retrieve that info as the only other way is two queries and this would most likely be more efficient.

That doesn't mean it never makes sense to auto-populate a MySQL date field as it you're just logging that info for retrieving later then it of course makes sense to do it within MySQL.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值