2012-06-12 84 views
2

我需要從最後一個插入中獲取story_id。我試過使用mysql_insert_id(),但它似乎不適用於我的聲明。下面是代碼:獲取插入查詢的最後一個ID

$stmt = $this->db->prepare("INSERT INTO active_stories (story_id, current_chapter, creator, cover_title, cover_img_name, create_date, last_update_date, story_tags, locked) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"); 
     $stmt->bind_param("issssssi",$zero, $creatorName,$storyTitle, $fileName, $createDate, $createDate, $themeTags, $zero); 
     $stmt->execute(); 
     printf("Last inserted record has id %d\n", mysql_insert_id()); 
     $stmt->close(); 
+2

同樣的問題在這裏問。 http://stackoverflow.com/questions/5057954/get-last-insert-id-after-a-prepared-insert-with-pdo – ExtremelyPoorMan

+0

謝謝,我試圖尋找這個,但不知道它當我做'$ storyID = $ db-> lastInsertId('story_id')時,我被稱爲PDO – mkral

回答

5

如果您正在使用PDO,here's how to get that

PDO::lastInsertId() 

如果使用的mysqli,try this one

mysqli::$insert_id 
+0

''我得到以下錯誤:'調用成員函數lastInsertId()在非對象上' – mkral

+0

使用'$ this- > db-> lastInsertId('story_id');' – ariefbayu

+0

嗯,我想我使用mysqli不是PDO。 – mkral