Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从mysql中的插入查询返回值?_Php_Mysql_Insert - Fatal编程技术网

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 typo
mysql\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