Re: Re: Getting last record ID created from DB
| From: | Chris | Date: | Tue, 20 Mar 2007 00:34:24 +0000 |
| Subject: | Re: Re: Getting last record ID created from DB | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250859@lists.php.net to get a copy of this message | ||
markw@mohawksoft.com wrote:
Check the documentation - currval returns the last one *for that session* - it does not return the last global change. http://www.postgresql.org/docs/current/static/functions-sequence.html It is perfectly safe to use this. -- Postgresql & php tutorials http://www.designmagick.com/markw@mohawksoft.com wrote:On Sat, 2007-03-17 at 12:19 -0400, Mark wrote:Yea, I've been using PG for over 10 years now. Sequences are awesome, but there have an "auto number" column type as well. It simply creates a sequence but it's there. I guess maybe most databases support the notion of "auto number" in some form. I haven't used MSSQL recently, but I think their solution is a GUID.There in lies the biggest problem with LAMP, and that's MySQL. The architecture of your methodology *only* works with MySQL, and not more capable databases like Oracle, DB2, or even PostgreSQL.I too thought the same thing, but was corrected when I starting working on a redesign for the billing system for the company that I am working for. They use PostgreSQL and we do have a auto incremented unique id field. It isn't call AUTO_INCREMENT, but rather a sequence value. It is a value that is controlled by the default value entry. It then runs this: nextval('customers_customer_id_seq'::regclass) and uses this as the value of the id field for the newly inserted row.Then instead of running a command like SELECT LAST_INSERT_ID(); you have to do some other stuff like this: $SQL = "SELECT currval('{$table}_{$column}_seq');";Again, this is absolutely wrong unless it is wrapped in a transaction. You will need a "begin" and a "commit" in an MVCC type system. If you don't wrap it in a transaction you don't know who got the last "nextval" of the sequence.