ibra has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm having a problem when trying to display the converted Unix time in a column. The code below is my sql statement which converts the UNIX format to a readable format. When running the sql query on a console it works perfect. However placing the converted date and time into a column seems to be the problem. I return an array called @transactions containing the results of the sql query.
my @transactions; my $sth = $db_h->Query(" SELECT serial_num, FROM_UNIXTIME(trans_date) AS 'Actio +n_date', actor, type, trans_data, content FROM transtable ORDER BY serial_num; "); $sth->execute(); while( my $trans_hash = $sth->fetchrow_hashref("NAME_lc") ) { push (@transactions,$trans_hash); } return @transactions;
Here I call the method returning the @transactions array. This is the line where I'm trying to print the date value
print " <td style='background-color:#f0f0f0;' ><b>$transaction->{'Act +ion_date'}</b></td>";
my @trans_array = (); my @trans_array = &Cpt::transactions_summary( $current_user ); foreach my $transaction (@trans_array) { print "<tr>"; print " <td style='background-color:#f0f0f0;' ><b>$transaction->{ +'serial_num'}</b></td>"; print " <td style='background-color:#f0f0f0;' ><b>$transaction->{ +'Action_date'}</b></td>"; + print " <td style='background-color:#f0f0f0;' ><b>$transaction->{ +'actor'}</b></td>"; print " <td style='background-color:#f0f0f0;' ><b>$transaction->{ +'type'}</b></td>"; print " <td style='background-color:#f0f0f0;' ><b>$transaction->{ +'trans_data'}</b></td>"; print " <td style='background-color:#f0f0f0;' ><b>$transaction->{ +'content'}</b></td>"; print "</tr>"; }
I really appreciate any feedback as I'm new to perl Thank you

Replies are listed 'Best First'.
Re: Problem converting with FROM_UNIXTIME
by rev_1318 (Chaplain) on Jun 23, 2011 at 13:22 UTC

    What output are you getting and what are you expecting?

    Besides, I'm not sure, but doesn't $sth->fetchrow_hashref("NAME_lc") mean that ALL fieldnames are in lower case?
    So I'm guessing that you want $transaction->{action_date} in stead of $transaction->{'Action_date'}...

    Paul

      If i edit this line
      FROM_UNIXTIME(trans_date) AS 'Action_date'
      And instead use just the column name
      SELECT serial_num, trans_date , actor, type, trans_data, content FROM + transtable
      The date values are displayed but in unix format.
        Sorry guys yep the problem was the lowercase issue. I complete missed this line
        $sth->fetchrow_hashref("NAME_lc")
        Thank you rev_1318 for spotting it
Re: Problem converting with FROM_UNIXTIME
by martell (Hermit) on Jun 23, 2011 at 13:17 UTC

    Hi

    I would like to help you, but you need to give some extra information. What is the exact perl module you are using: DBI, Class::DBI, DBIx::Class ... ? And on what kind of databank are you running: Postgresql, Oracle, mySQL ... ? And what is the exact output resulting from print statement: an error, a string with nothing in, something like HASHx.., ... ?

    In my experience, the problem is often the correct use of the quotations in the statement. For example, when using mySQL, if find using backtick quotes ` on every column a good rule to avoid problems. Also use the qq{} notation to avoid any change of misinterpretation by perl when parsing your select statement, especially when using using variables (which you are not doing here, but better save than sorry).

    So, try once:

    my $sth = $db_h->Query(qq{ SELECT `serial_num`, FROM_UNIXTIME(`trans_date`) AS `Action_date +`, `actor`, `type`, `trans_data`, `content` FROM `transtable` ORDER B +Y `serial_num`; });

    Kind Regards

    Martell

    update:

    Try first the suggestion of rev_1318 below. Didn't spot the issue in his remark, but after reading it, I think he's right.