简体   繁体   中英

MySQL datetime show 0000 with PHP PDO

I'm having troubles trying to insert a timestamp into MySQL always shows 0000-00-00 00:00:00

The scheme for the table is this:

CREATE TABLE `Rawdata` (
`Exchanger` varchar(30) NOT NULL,
`Timestamp` datetime NOT NULL,
`Last` float NOT NULL,
`Bid` float NOT NULL,
`Ask` float NOT NULL,
 PRIMARY KEY (`Exchanger`,`Timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

And the code of the php :

$query = "INSERT INTO Rawdata (Exchanger,Timestamp,Last,Bid,Ask) VALUES(?,?,?,?,?)";
    $result = $link->prepare($query);
    $result->bindParam(1,$exchanger,PDO::PARAM_STR);
    $result->bindParam(2,$timestamp,PDO::PARAM_STR);
    $result->bindParam(3,$last,PDO::PARAM_STR);
    $result->bindParam(4,$bid,PDO::PARAM_STR);
    $result->bindParam(5,$ask,PDO::PARAM_STR);
    $result->execute();

Also this is the var_dump() of the variables

array(5) { [1]=> string(6) "Mt.Gox" [2]=> string(10) "1391036493" 
[3]=> string(6) "929.00" [4]=> string(6) "929.00" [5]=> string(6) "932.90" } 

I don't know why is not working.. Because in other function i update other table with the same timestamp column and it works :/

What can be wrong?

Thanks in advance

Edit:

The $timestamp variable comes from this:

$apivariable = string(16) "1391038069839089"

then i use the following:

$timestamp = time($apivariable)

Pastebin:

http://pastebin.com/vBn0TBvG

Instead of using:

$result->bindParam(2,$timestamp,PDO::PARAM_STR);

Use:

$result->bindValue(2,date('Y-m-d H:i:s', $timestamp),PDO::PARAM_STR);

But your column in database has not type timestamp

Timestamp datetime NOT NULL

To insert datetime value should be in "0000-00-00 00:00:00" format.

Try $result->bindValue(2,date('Ymd H:i:s', $timestamp),PDO::PARAM_STR);

$timestamp是unix时间戳,您需要将其格式化/转换为适当的mysql类型。

INSERT INTO Rawdata (Exchanger,Timestamp,Last,Bid,Ask) VALUES(?,FROM_UNIXTIME(?),?,?,?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM