简体   繁体   English

为什么 PDO::lastInsertId 返回 0?

[英]Why does PDO::lastInsertId return 0?

This is my code.这是我的代码。

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

My log says:我的日志说:

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

However the insert query is successful, the database record is created and both errorInfo outputs give no error.但是插入查询成功,创建了数据库记录,并且两个 errorInfo 输出都没有错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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