http://www.perlmonks.org?node_id=615698

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

CAn u plz guide a solution. I want to convert unix_timestamp into timestamp format which is readable. the value should be inserted into database mysql which is the timestamp datatype. I am not able to insert into database if i convert unix_timestamp to localtime(). the inserted value got as NULL
$query = "select unix_timestamp(now())- unix_timestamp(timestamp) from + test; $query1= $dbh->prepare($query); $query1->execute(); while(@row_time = $query1->fetchrow_array()){ $old_time=$row_time[0]; } $tt= localtime($old_time); $quer= $dbh->do("insert into test1 values(" . $tt .")"); print "$quer"; $dbh->disconnect; exit(0);

Replies are listed 'Best First'.
Re: Timestamp problem
by Tomte (Priest) on May 16, 2007 at 08:07 UTC

    First things first:
    Please ask your questions in english...this forum is an international effort, writing correct english as best as you can is not only polite but increases the chance of a helpful answer significantly - errors are normal and anticipated, but constructs as "CAn u plz" are frowned upon.

    On to your problem:

    • Use placeholders, don't concatenate SQL statements and values
    • Use mysql onboard functions everywhere you can, there is an inverse function to the "unix_timestamp()" you are using, and thats "from_unixtime()"
    Untested example to get you started:
    [...] my $stmt = $dbh->prepare("insert into test1 values(FROM_UNIXTIME(?))") + or die("couldn't prepare insert: " . $@); $stmt->execute(time()-$old_time) or die ("couldn't perform insert: " . + $@); [...]
    Consult The documentation available to repair my errors...

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

    A reply falls below the community's threshold of quality. You may see it by logging in.