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.
Related Articles...
More Articles...
Popular Posts
- Software Testing Quiz Questions and Answers
- Javascript Quiz Questions and Answers
- MySQL Quiz Questions and Answers
- SEO Your Blog the Easy Way - Guest Post
- PHP Quiz Questions and Answers
- SEO (Search Engine Optimization) Quiz Questions and Answers.
- HTML Quiz Questions and Answers
- Use of tinyurl in Twitter
- Basic Computer Hardware Quiz Questions and Answer.
- Best Motivational Quotes Book | Kindle eBook and Paperback | Read for FREE with Amazon's Kindle Unlimited

1 comment:
mysql_insert_id() this is the best solution, I use this code extensively on my projects.
Post a Comment