Friday, 10 August 2018

Discover Auto Increment ID After MySQL Insert With PHP

Inserting a value into a database with an auto incrementing field is quite common. Once you insert the new row you would expect that you need to do another query to get the newly created ID.
Another option is to use the mysql_insert_id() function to retrieve the ID created by the last insert statement.
  1. // insert
  2. $sql = 'INSERT INTO table(colum1, colum2) VALUES(1, 2);';
  3. mysql_query($sql);
  4.  
  5. // get new id
  6. $id = mysql_insert_id();
The $id variable will now contain your most recently inserted ID.

0 comments:

Post a Comment