Re: equivalent to mysql_insert_id()
| From: | Christopher J. Mackie | Date: | Mon, 05 Jul 2004 19:27:15 +0000 |
| Subject: | Re: equivalent to mysql_insert_id() | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-13495@lists.php.net to get a copy of this message | ||
Lukas Smith wrote:
...
> this should work:
> mysql_insert_id($db->connection)
In fact, this doesn't appear to work--on my system, it returns an error
saying that $db is not a MySQL-Link resource. Dunno if that's true
generally, or a quirk of my (Windows) setup.
You can do this by hacking MySQL.db. The following *does* work (based on
the code for DB::affectedRows). This solution is not robust--it doesn't
check carefully to see if it's an insert query (the 'isManip' test is for
insert, update and/or delete), and I don't understand the DB::MySQL code
well enough to know how and when what I've done will break. It would also
be cleaner to subclass DB/MySQL, rather than hack it directly. But if you
need this function very badly, the code is a starting point.
--CJ
// }}}
// {{{ insertID()
/**
* Gets the ID affected by an insertquery.
* For other queries, this function returns 0.
*
* @return ID (primary key) number of row affected by the last query
*/
function insertID()
{
if (DB::isManip($this->last_query)) {
$result = @mysql_insert_id($this->connection);
} else {
$result = 0;
}
return $result;
}