Re: the Pear::DB way to do mysql_insert_id?
| From: | Stig S. Bakken | Date: | Wed, 04 Dec 2002 23:53:32 +0000 |
| Subject: | Re: the Pear::DB way to do mysql_insert_id? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-2897@lists.php.net to get a copy of this message | ||
On Mon, 2002-12-02 at 16:06, Jeff Bearer wrote:
> What is the proper PEAR way to get the value that mysql_insert_id()
> gives you when using an auto_increment index.
>
> I'm assuming that the function isn't implemented that way because other
> databases don't do sequences the same way. I looked through the
> nextId() function and it uses another table to keep the sequence.
>
> Could somebody explain the way this should be implemented with mysql?
Not all databases have auto_increment columns. Because of this, we can
not use this concept in PEAR DB. The portable approach is to define
sequences, and emulate them on platforms without native sequences. This
is what nextId does. But this also means that you can't slap DB on top
of an existing application using auto_increment and expect it to work
cleanly (you need to hack around with mysql-specific functions then).
So, you should think sequences instead of thinking auto-increment. Here
is the PEAR way of doing it:
$id = $dbh->nextId("table");
$res = $dbh->query("INSERT INTO table VALUES(?,?)", array($id, $name));
- Stig