我在一个创建新“视频”的Code Igniter模型中有这个函数。
// Creates a new video.
public function newVideo($title, $description) {
$this->db->query("INSERT INTO videos VALUES (NULL, '$title', '$description')");
return // id of this new row
}如何在我的MySQL表中获得这个新行的ID?我可以得到最后一行并加1,但我认为可能存在并发错误。
发布于 2012-03-23 08:21:09
非常简单的答案并不需要超过30个字符,不是吗?
$this->db->insert_id();发布于 2012-03-23 08:20:36
也许你需要这样的东西
$this->db->insert_id(); 发布于 2012-03-23 08:22:07
试试这样的东西:
$lastID = -1; //That's where it'll be stored
$query = "SELECT LAST_INSERT_ID()";
$result = mysql_query($query);
if ($result)
{
$row = mysql_fetch_row($result);
$lastID = $row[0];
}https://stackoverflow.com/questions/9832461
复制相似问题