Lets say you have inserted a row inside your mysql table from PHP, and now you need the ID value that just inserted as auto incremental field. Mysql has a function that returns the last inserted auto incremental field. The function is LAST_INSERT_ID()
You can easily call the function from your PHP code. I'm providing the PHP code for you that will retrieve the last auto incremented field from mysql.
That's it. This will need you when you have inserted a row at mysql, and the auto incremented field is a foreign key of another table. That case you just get the ID using above method, and use at that table.
You can easily call the function from your PHP code. I'm providing the PHP code for you that will retrieve the last auto incremented field from mysql.
## get the last inserted auto increment id from mysql table
$query = "SELECT LAST_INSERT_ID() ID";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$id = $row['ID'];
}
That's it. This will need you when you have inserted a row at mysql, and the auto incremented field is a foreign key of another table. That case you just get the ID using above method, and use at that table.
Comments
Thank you for posting it!