RE: [PHP] Add a record and recover it's ID in one operation

From: Date: Thu, 13 Feb 2003 22:13:45 +0000
Subject: RE: [PHP] Add a record and recover it's ID in one operation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-135530@lists.php.net to get a copy of this message
--- Chris Kay <Chris.Kay@techex.com.au> wrote: > > Something like this perhaps.... untested > > function insert($query) { > // Connect info > mysql_connect("host","user","pass"); > > // Select DB > mysql_select_db("DBname"); > > // Perform insert > $indb=mysql_query($query); > > // Gets record ID > $dbq=mysql_query("select last_insert_id()"); > $data=mysql_fetch_row($dbq); > > // Return ID > return $data[0]; > } You can replace everything after "// Gets record ID" with this: return mysql_insert_id(); Chris

« previous php.general (#135530) next »