创建新行条目时从mysql行检索时间戳 [英] Retrieving timestamp from mysql row upon creation of new row entry

查看:134
本文介绍了创建新行条目时从mysql行检索时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效吗?

    <?php
        $timestamp = mysql_query("INSERT INTO column1 (entry1, entry2) 
                                  VALUES ('$entry1', '$entry2') 
                                  AND SELECT timestamp");
    ?>

timestamp是mysql列,在创建行时会生成时间戳。

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).

谢谢!

推荐答案

我取决于您需要的信息。如果您需要将它存储在其他行/实体中,并且希望保持一致,那么我将日期存储在PHP中(可能在Web应用程序中每个请求一次),然后在整个日期中使用它,例如

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.

这并不意味着自动填充MySQL日期字段永远没有任何意义,因为您只是记录该信息以供日后检索那么在MySQL中执行此操作当然很有意义。

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.

这篇关于创建新行条目时从mysql行检索时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆