3

Possible Duplicate:
PHP: how to get last inserted ID of a table?

This is my query for inserting a new post into the posts table (PHP/mysql). Can I return the post_id for the post just inserted in the same statement? I need the post_id for insertion into another table.

mysql_query("INSERT INTO `posts` (`post_id`, `date`, `title`, `content`) VALUES (NULL,    NOW(), '$title', '$fileName')");
Community
  • 1
  • 1
user1481563
  • 125
  • 1
  • 2
  • 7

3 Answers3

0

mysql_insert_id() will give you the last ID

christian.thomas
  • 1,122
  • 1
  • 8
  • 19
0

This is exactly what the MySQL function last_insert_id() is for. So, essentially the answer is, "no, but kinda". You need to execute an additional query "select last_insert_id()", which will return 1 row, with your ID in it.

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
0
mysql_query("INSERT INTO `posts` (`date`, `title`, `content`) VALUES (NOW(), '$title', '$fileName')");
$last_inserted = mysql_insert_id(); // return last inserted id
sel
  • 4,982
  • 1
  • 16
  • 22