开发者

Why does MySQL DATE_FORMAT print a blank result?

开发者 https://www.devze.com 2023-01-15 00:52 出处:网络
For the past couple of hours I\'ve been trying to format a MySQL timestamp using DATE_FORMAT and it doesn\'t do anything!

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 timesta开发者_运维问答mp just fine, albeit not formatted though. Can somebody provide their insight on this matter, please?


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消