Why does PDO::lastInsertId return 0?

▼魔方 西西 提交于 2019-12-24 01:17:54

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/6558885/why-does-pdolastinsertid-return-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!