2012-06-12 53 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