Re: [Phplib-users] nextid - how does it work?
Brought to you by:
nhruby,
richardarcher
From: Nathaniel P. <np...@te...> - 2003-07-16 22:24:53
|
----- Original Message ----- From: "Liberty Young" <li...@em...> To: "Neha Mathur" <ne...@sp...> Cc: <php...@li...> Sent: Wednesday, July 16, 2003 3:02 PM Subject: Re: [Phplib-users] nextid - how does it work? > I can't answer your question..though i do have a few comments. > > Why are you inputting primary keys? Few RDBMS don't have the option to > auto-increment your primary key field. I rarely find myself in a > situation where i'm inputing the value for a primary key field. Foreign > keys, yes, but never primary keys!!! If you're inserting a new record, > let your server do that for you. I _know_ MySQL will do that for you There are some instances where it is useful or even necessary to know ahead of time what primary key will be assigned to a given record when it is inserted. This function provides one means of doing that. I don't know about MySQL, but in PostgreSQL you can use built-in sequences and find out the primary key that will be used by querying the database using "select nextval('sequence_name');", however, since this is not part of the SQL standard, it is likely not portable to other databases. This method would be portable across all the databases PHPlib supports. It also results in a bit cleaner code; using PostgreSQL, I have to do this to get a primary key from the database: $db->query("select nextval('sequence_name');"); $db->next_record(); $my_id = $db->f('nextval'); whereas with PHPlib it would become: $my_id = $db->nextid('sequence_name'); _________________________________ Nathaniel Price <np...@te...> Webmaster |