获取插入查询的最后一个ID

时间:2012-06-12 15:44:40

标签: php mysql insert mysql-insert-id

我需要从最后一次插入中获取story_id。我曾尝试使用mysql_insert_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();

1 个答案:

答案 0 :(得分:5)

如果您使用的是PDO here's how to get that

PDO::lastInsertId()

如果使用mysqli,try this one

mysqli::$insert_id