On Wed, 2004-05-05 at 22:49, Paul DuBois wrote: > At 22:39 -0400 5/5/04, Nathan Jones wrote: > > > Hi there, > >> > >> I seem to be having a problem retrieving the last inserted ID for a > >> table. > >> > >> The query I am using is as follows: > >> > > > >It's far easier than anyone else has mentioned, as of my writing. Just > >use the PHP function 'mysql_insert_id()' after your insert query. This > >function has been in the PHP MySQL module since at least sometime during > >the version 3 cycle. Here's the address to the online PHP documentation > >for the MySQL module: http://www.php.net/manual/en/ref.mysql.php > > This is true, but it's possible that the underlying cause of the problem > that results in the query not working on PHP may also affect whether or > not mysql_insert_id() returns the desired value. For example, if the > SELECT LAST_INSERT_ID() query is not being issued on the same connection > as the query that generates the ID, calling mysql_insert_id() might not > work, either.
mysql_insert_id() takes as an optional parameter, like all MySQL functions in PHP, referencing the connection to check. To garauntee that you are using the correct connection, always pass the resource for the connection to the mysql_* functions. mysql_insert_id() must be called as the next query on the connection of the INSERT query, otherwise you get back 0. By using this function, you simply reduce the chances for error in the id retrieval. I know that by default many of us don't RTFM, but in the case of any language, I've found that reading the manuals to be very helpful. In fact, I keep copies of all the manuals locally in order to save the time required to retrieve from the web. > > -- > Paul DuBois, MySQL Documentation Team > Madison, Wisconsin, USA > MySQL AB, www.mysql.com -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]