It is necessary to get the ID (auto increment field) of a row inserted into a mysql table if the table is having any parent-child relationship with one or more child tables.
For example, assume that we are entering questions and multiple answers. The questions will be inserted into question table. The id in question table will be used in answer table for relating the question with answers.
So we need to get the id from questions table once after inserting the question.
We can use many different approaches to achieve this.
1. We can have a php variable assigned with current time, this variable value can be stored in question table as hash.
Once after completing the question insertion we can select the id corresponding to this hash from the question table. This approach needs one additional column in the table for storing the hash.
2. We can select the id as “select max(id) from question”. This approach will work only when the id is getting incremented continuously.
3.The best way is we can use the php function Mysql_insert_id() to retrieve the ID generated for an AUTO_INCREMENT column by the previous/last INSERT query.