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.
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)
";
精彩评论