php pdo datetime,MySQL日期时间显示0000与PHP PDO(MySQL datetime show 0000 with PHP PDO)

MySQL日期时间显示0000与PHP PDO(MySQL datetime show 0000 with PHP PDO)

我有麻烦试图插入时间戳到MySQL总是显示0000-00-00 00:00:00

该表的方案是这样的:

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

和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();

这也是变量的var_dump()

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" }

我不知道为什么不工作..因为在其他功能我更新其他表与相同的时间戳列,它的工作原理:/

有什么不对?

提前致谢

编辑:

$timestamp变量来自:

$apivariable = string(16) "1391038069839089"

然后我使用以下内容:

$timestamp = time($apivariable)

引擎收录:

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:

原文:https://stackoverflow.com/questions/21444662

更新时间:2019-10-31 00:34

最满意答案

而不是使用:

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

使用:

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

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

2014-01-29

相关问答

你的配置 允许外部通过URL链接才行啊。或者你通过一个二级域名指定目录来访问也行

而不是使用: $result->bindParam(2,$timestamp,PDO::PARAM_STR);

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

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

Use: $result->bindValue(2,date('Y-m-d H:i:s', $time

...

这经常发生。 您将mysql timestamp混淆了,它实际上是一个DATETIME类似的值,它是UNIX时间戳记,这是自1970-01-01以来的秒数。 您需要更改字段格式或插入值。 你也可以方便地找到一些mysql函数 - 例如CURDATE()或NOW() $sql = 'INSERT INTO users VALUES (NULL,:username,:password,:email,NOW(),NOW())';

$stmt = $pdo->prepare($sql);

$stmt->e

...

@Peter Darmis的评论是正确的。 PHP 5.3.3没有完全卸载,不知道为什么。 已经手动安装了PHP进行编译,因为我不知道这个,我使用yum来安装模块,当时安装了PHP 5.3.3,我不知道为什么要点un-install正常。 所以我最终得到了2个PHP版本,因此卸载PHP 5.3.3完全使用yum然后解决了这个问题用pdo选项编译PHP @Peter Darmis was right from his comments. PHP 5.3.3 was not completely un

...

你的架构开始是错误的。 问题不在于设置日期本身,这显然是无效的,MySQL和PHP都拒绝它,因为没有0年和0月没有0,你看到的输出只是对一种有效的日期(1年1月1日和1月1日之前的1天)。 但是如果你做得对的话,你也只是错过了Doctrine摘录它的观点: $entEmail = new Email();

$entEmail->setViewedAt(null);

Doctrine现在很乐意将NULL放在数据库列中。 Your architecture is wrong to begin wit

...

您在foreach循环的每次迭代中都执行查询。 查看更新。 尝试更换 foreach ($db->query($sql) as $row) { ...

同 $result = $db->query($sql);

foreach ($result as $row) {

更新:@mario是正确的。 foreach不会评估每次迭代的表达式。 我似乎无法找到一个确切的答案,为什么这会解决OP问题; 我仍然认为它有一些东西,但即使在我自己的测试中,似乎使用该变量似乎对性能没有任何显着影响。 如果有人有

...

该备份脚本很荒谬,没有人应该制作它的另一个版本。 我之前看过这个脚本,以及类似的尝试,他们有很多问题: 不在分支中划分表名 不处理NULL 不处理字符集 不处理二进制数据 不备份视图 不备份TRIGGER或存储过程或存储的功能或事件 使用过时的mysql扩展(但这就是为什么你想要一个PDO版本,不是吗?) 使用addslashes()代替正确的MySQL转义函数。 在输出整个内容之前,将所有表格的所有数据追加到一个非常长的字符串中。 这意味着您必须能够将整个数据库存储在一个字符串中,这几乎肯定会泄

...

问题#1 - 我不确定如何限制用户查看某些类别或控制他们对这些类别的访问权限。 答 :为用户创建一些角色并将这些角色分配给每个用户。 根据用户的角色,您可以限制用户查看特定类别。 问题#2 - 我已经计算出如何将doc分配给一个类别,但只有1个,我需要的是'doc'有需要时可以成为多个组的一部分。 回答 :在您的doc_list表中,将cat_no数据类型更改为文本,并将所有类别值定义为逗号分隔。 例如:对于doc1,cat_no将是1,2,3(其中1 2和3是不同的类别ID)。 然后,您可以使用

...

SET NAMES 'UTF8'语句是正确的,但您必须每次都执行它。 您可以指示PDO自动运行命令: $dsn = sprintf( 'mysql:dbname=%s;host=%s', DB_NAME, DB_HOST );

$driver_options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' );

try {

$dbc = new pdo($dsn, $user, $pw, $driver_options);

...

不应引用占位符名称。 删除周围的引号:date和:text ,因此它变为: "INSERT INTO `vk_posts` (`date`, `text`) VALUES (:date, :text)"

The placeholder names should not be quoted. Remove the quotes around :date and :text, so it becomes: "INSERT INTO `vk_posts` (`date`, `text`) VALUES

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值