Re: PEAR::DB_Common::nextId()

From: Date: Thu, 15 Jan 2004 08:18:55 +0000
Subject: Re: PEAR::DB_Common::nextId()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-174629@lists.php.net to get a copy of this message
Alessandro Vitale wrote:
I would like to get the last insert id... anyone has some experience in using the PEAR::DB_Common::nextId() ? Yes. You have a PEAR DB object $db, and you do:
$id = $db->nextID('Documents'); $db->query("INSERT INTO Documents (id,title,text) VALUES ($id,'Title','Text')"); The confusing thing about this is the fact that "Documents" in the two lines don't necessarily have anything to do with each other. The first is the name of a sequence, the other is the name of a table. They're just both called 'Documents'. What happens, and I believe this isn't described in the documentation, is that PEAR DB stores the current ID of the Documents sequence in a separate table called Documents_seq: mysql> select * from Documents_seq; +-----+ | id | +-----+ | 200 | +-----+ So it's a completely different mechanism than using mysql_insert_id(). In fact, it might be a better idea to have just one sequence and generate IDs for all tables from that: $id = $db->nextID('Sequence'); $db->query("INSERT INTO Documents (id,title,text) VALUES ($id,'Title','Text')"); $id = $db->nextID('Sequence'); $db->query("INSERT INTO Users (id,username...) VALUES ($id,$username...)");
any suggestion would be very much appreciated. alessandro


« previous php.general (#174629) next »