Re: Sequences in DB
| From: | Colin Viebrock | Date: | Tue, 03 Apr 2001 15:24:51 +0000 |
| Subject: | Re: Sequences in DB | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-155@lists.php.net to get a copy of this message | ||
> It was mentioned in one of the replies to the mysql_insert_id to use
> sequences which are provided by DB. Could someone please tell me how to
> use this feature and how they are implemented. I know you get sequences in
> Oracle and Postgress but these are part of the database. How does DB
> handle this as it is db independant?
As someone who misses mysql_insert_id() ... this is what I did:
1) Drop the AUTO_INCREMENT from your tables, e.g.:
ALTER TABLE myTable MODIFY id INT UNSIGNED NOT NULL DEFAULT 0;
2) Instead of:
$res = $db->query("INSERT INTO myTable (text) VALUES ('foo')");
$last_id = mysql_insert_id($db->connection);
Start doing this:
$id = $db->nextID('myTable');
$res = $db->query("INSERT INTO myTable (id,text) VALUES ($id,'foo')");
PEAR will generate a myTable_seq table for you that handles the sequences.
Bascially it's a "next id" instead of a "last id".
- Colin