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...
Shop at Rajamanickam.com | Birthday Gift Idea? | Hire me for $6 per Hour
Get 3 useful ebooks for Rs 99 in India and $5.99 globally
Get a 75% commission | ChatGPT and and Google Gemini for Beginners (Use Discount code QPT)
Search This Blog
Art of Talking to AI | Tech eBook | Dream Big | Listen to Dream Big
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
1 comment:
mysql_insert_id() this is the best solution, I use this code extensively on my projects.
Post a Comment