显示在mysql中创建行的日期 [英] Display the date of creation of a row in mysql

查看:83
本文介绍了显示在mysql中创建行的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户表中添加了新行,以显示每个用户的创建日期. 我为此尝试了时间戳和日期时间类型,但仍然在每个用户旁边显示0000-00-00 00:00:00.

I added a new row to my users' table to show the date of creation of each user. I tried both timestamp and datetime types for that but it still displays me 0000-00-00 00:00:00 next to each user.

这是将行添加到用户表中的代码

here's my code for adding the rows to the users's table

$first_name = trim($_REQUEST['first_name']);
$last_name = trim($_REQUEST['last_name']);
$username = trim($_REQUEST['username']);
$password = trim($_REQUEST['password']);
$email = trim($_REQUEST['email']);
$created = strftime("%Y-%m-%d %H:%M:%S", time());
$insert_sql = sprintf("INSERT INTO users (first_name, last_name, username, password, email, created)" .
"VALUES('%s', '%s', '%s', '%s', '%s', '%s');",
mysql_real_escape_string($first_name),
mysql_real_escape_string($last_name),
mysql_real_escape_string($username),
mysql_real_escape_string(crypt($password, $username)),
mysql_real_escape_string($email),
mysql_real_escape_string($created));
mysql_query($insert_sql) or die(mysql_error());

这是显示信息的代码

$select_users = "SELECT * FROM users";
$result =  mysql_query($select_users);
while ($user = mysql_fetch_array($result)) {
$user_row = sprintf(
"<td><input type='checkbox' name='checkbox[]' id='checkbox[]' value='%d'/></td>" .
"<td>%s %s</td> " .
"<td>%s</td>" .
"<td><a href='mailto:%s'>%s</a></td> " .
"<td>%s</td>" .
"<td><a href='javascript:delete_user(%d);'><img class='delete_user' src='images/trash.png' alt='' title='' border='0' /></a></td>",
$user['user_id'], $user['first_name'], $user['last_name'], $user[username],
$user['email'], $user['email'], $user['created'], $user['user_id']);
echo $user_row;

除了显示日期为0000-00-00 00:00:00的创建日期外,我所有字段均正确显示. 我不知道我做错了什么. 谢谢您的帮助.

I got all the fields displayed correctly except for the creation date which shows 0000-00-00 00:00:00 . I don't know what I did wrong. Thank you for the help.

我已经有一个名为更新"的字段,该字段使用当前时间戳".

EDIT : I already have a field called "updated" which uses "Current_timestamp".

推荐答案

除了可以使用php设置时间外,您还可以在数据库中创建一个名为created的字段,其类型为timestamp,并将默认值设置为CURRENT_TIMESTAMP.这将自动执行您需要的操作,而无需使用php. 这是代码:

Instead of setting the time with php, you could make a field in the database named created with type of timestamp and set default value to CURRENT_TIMESTAMP. This will automaticly do what you need without php. Here is the code:

    ALTER TABLE `users` ADD COLUMN `created` timestamp DEFAULT CURRENT_TIMESTAMP;

这篇关于显示在mysql中创建行的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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