为什么PDO :: lastInsertId返回0? [英] Why does PDO::lastInsertId return 0?

查看:87
本文介绍了为什么PDO :: lastInsertId返回0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.

// insert reward into wallet
$sql = "
INSERT INTO `wallet` (`uid`, `created_at`, `amount`, `type`, `payment_id`) VALUES (:uid, CURRENT_TIMESTAMP, :amount, 'payment', :payment_id);
";
$sth = self::link()->prepare($sql); 
// primary key makes sure payment does not get double rewarded
$sth->execute(
    array(
    ':uid' => $referer,
    ':amount' => $reward,
    ':payment_id' => $payment_data['payment_id'],
    )
);
var_dump(self::link()->errorInfo());
self::log("issuing subscription",self::LOG_LEVEL_DEBUG);
// extend referers subscription
$tid = self::link()->lastInsertId();
var_dump(self::link()->errorInfo());
self::log("using $tid as id for wallet transfer",self::LOG_LEVEL_DEBUG);

我的日志说:

[2011-07-02 20:31:44] using 0 as id for wallet transfer

但是,插入查询成功后,将创建数据库记录,并且两个errorInfo输出均未给出错误.

However the insert query is successful, the database record is created and both errorInfo outputs give no error.

推荐答案

从查询中删除分号或分号后的空格,或两者都删除:

Remove the semicolon or the whitespace after the semicolon or both from your query:

$sql = "
INSERT INTO `wallet` (`uid`, `created_at`, `amount`, `type`, `payment_id`) VALUES (:uid, CURRENT_TIMESTAMP, :amount, 'payment', :payment_id)
";

这篇关于为什么PDO :: lastInsertId返回0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆