为什么 MySQL DATE_FORMAT 打印空白结果?

发布于 2024-09-18 22:06:45 字数 698 浏览 7 评论 0原文

在过去的几个小时里,我一直在尝试使用 DATE_FORMAT 格式化 MySQL 时间戳,但它没有做任何事情!

Perl 代码:

use CGI;
use DBI;

my $q = new CGI;

# Database connection goes here

my $sth_select = $dbh->prepare(
    "SELECT DATE_FORMAT(timestamp, '%m/%d/%y') FROM foo"
);

$sth_select->execute() || die "Unable to execute query: $dbh->errstr";

if (my $ref = $sth_select->fetchrow_hashref()) {
    print $q->header;
    print " TIME: $ref->{timestamp}";
    exit;
}

结果

TIME: 

它根本不打印格式化的时间,它是空白的

当我尝试打印时间戳时,它不会打印任何内容,但如果我要删除 DATA_FORMAT 并只是简单地执行 SELECT timestamp FROM foo ,那么它会打印时间戳很好,虽然没有格式化。有人可以就这个问题提供他们的见解吗?

For the past couple of hours I've been trying to format a MySQL timestamp using DATE_FORMAT and it doesn't do anything!

Perl Code:

use CGI;
use DBI;

my $q = new CGI;

# Database connection goes here

my $sth_select = $dbh->prepare(
    "SELECT DATE_FORMAT(timestamp, '%m/%d/%y') FROM foo"
);

$sth_select->execute() || die "Unable to execute query: $dbh->errstr";

if (my $ref = $sth_select->fetchrow_hashref()) {
    print $q->header;
    print " TIME: $ref->{timestamp}";
    exit;
}

Results

TIME: 

It doesn't print the formatted time at all, it is blank!

When I attempt to print the timestamp it doesn't print anything, but if I were to remove DATA_FORMAT and just simply do a SELECT timestamp FROM foo, then it prints the timestamp just fine, albeit not formatted though. Can somebody provide their insight on this matter, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆消瘦 2024-09-25 22:06:45

返回的散列具有数据库提供的键列标题。当使用这样的函数时,列标题实际上是“DATE_FORMAT(timestamp, '%m/%d/%y')”。
尝试将您的 SQL 修改为:

my $sth_select = $dbh->prepare("SELECT DATE_FORMAT(timestamp, '%m/%d/%y') AS timestamp FROM foo");

The hash returned has as keys column headers as provided by the database. When using a function like that, the column header is actually "DATE_FORMAT(timestamp, '%m/%d/%y')".
Try modifying your SQL to be:

my $sth_select = $dbh->prepare("SELECT DATE_FORMAT(timestamp, '%m/%d/%y') AS timestamp FROM foo");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文