Page 1 of 1
Creating an id number?
Posted: Sun Oct 15, 2006 6:06 pm
by cturner
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.
Posted: Sun Oct 15, 2006 6:11 pm
by wtf
select max(id) will return the largest id from the database you can just add 1 to it for the next available
Posted: Sun Oct 15, 2006 6:40 pm
by volka
wtf wrote:select max(id) will return the largest id from the database you can just add 1 to it for the next available
In this case you have to lock the table or you will get a
race condition.
You should let the database handle such ids. What database system or abstraction layer do you use?
Posted: Sun Oct 15, 2006 6:53 pm
by cturner
volka and wtf I have looked at what you have posted and it will help but I am wanting to create the id number before I have to retrieve it from the database table. How do I create the id number? I am using mysql.
Posted: Sun Oct 15, 2006 6:56 pm
by volka
Why do you want to create this id before you have inserted any data?
Posted: Sun Oct 15, 2006 7:00 pm
by cturner
So I know which comment belongs to which article.
Is this the right way to do it?
Posted: Sun Oct 15, 2006 7:06 pm
by volka
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
article:
yadda yadda yadda
[click here to comment this article]
Here you already need to know the article_id or you won't know for which article the button was pressed.
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