Creating an id number?
Moderator: General Moderators
Creating an id number?
I am wanting to create an id number and then add to a database table. The reason why I am wanting to do this is because I want the user to add a comment then when that is done I want a id number to be added to each of two tables so it can be retrieved when the comments for a particular article are being viewed. Can someone please show me how I can do this? Thanks in advance.
In this case you have to lock the table or you will get a race condition.wtf wrote:select max(id) will return the largest id from the database you can just add 1 to it for the next available
You should let the database handle such ids. What database system or abstraction layer do you use?
First there is the article, this article has an id, let's call the field article_id.
Now someone wants to write a comment for this article. This is probably signaled by pressing some kind of button or link
Make article_id an auto_incrment field. If you need to know the value right after an INSERT statement use e.g. http://de2.php.net/manual/en/function.m ... ert-id.php
Now someone wants to write a comment for this article. This is probably signaled by pressing some kind of button or link
Here you already need to know the article_id or you won't know for which article the button was pressed.article:
yadda yadda yadda
[click here to comment this article]
Make article_id an auto_incrment field. If you need to know the value right after an INSERT statement use e.g. http://de2.php.net/manual/en/function.m ... ert-id.php