Php 从mysql中的插入查询返回值?
可能重复:Php 从mysql中的插入查询返回值?,php,mysql,insert,Php,Mysql,Insert,可能重复: 这是我在posts表(PHP/mysql)中插入新帖子的查询。我可以返回刚刚插入同一语句中的帖子的帖子id吗?我需要post_id以便插入到另一个表中 mysql_query("INSERT INTO `posts` (`post_id`, `date`, `title`, `content`) VALUES (NULL, NOW(), '$title', '$fileName')"); 将为您提供最后一个ID将为您提供最后一个ID这正是MySQL函数last\u inse
这是我在posts表(PHP/mysql)中插入新帖子的查询。我可以返回刚刚插入同一语句中的帖子的帖子id吗?我需要post_id以便插入到另一个表中
mysql_query("INSERT INTO `posts` (`post_id`, `date`, `title`, `content`) VALUES (NULL, NOW(), '$title', '$fileName')");
将为您提供最后一个ID将为您提供最后一个ID这正是MySQL函数last\u insert\u ID()的用途。所以,基本上答案是,“不,但有点”。您需要执行另一个查询“select last_insert_id()”,该查询将返回1行,其中包含您的id 这正是MySQL函数last\u insert\u id()的用途。所以,基本上答案是,“不,但有点”。您需要执行另一个查询“select last_insert_id()”,该查询将返回1行,其中包含您的id 第一条评论中的@Michael typo
mysql\u insert\u id()
或选择最后一条评论中的@Michael typomysql\u insert\u id()
或选择最后一条评论中的@Michael typo
mysql_query("INSERT INTO `posts` (`date`, `title`, `content`) VALUES (NOW(), '$title', '$fileName')");
$last_inserted = mysql_insert_id(); // return last inserted id