2011-07-02 116 views
1

這是我的代碼。爲什麼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中輸出給沒有錯誤。

+7

是表中的任何AUTO_INCREMENT領域?當你的表中存在一個auto_increment字段時,lastInsertId可以工作 –

+2

也不需要在你的查詢中使用分號......它絕對不是必需的,我甚至可能會使查詢失敗。你確定成功了嗎? – prodigitalson

+2

你釘了它,謝謝。如果你可以張貼這個答案,所以我可以upvote和接受。 –

回答

1

從您的查詢分號或兩個分號後或空白:

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