Re: mdb2 help
| From: | Ferenc Kovacs | Date: | Thu, 04 Aug 2011 08:50:01 +0000 |
| Subject: | Re: mdb2 help | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-31189@lists.php.net to get a copy of this message | ||
On Thu, Aug 4, 2011 at 1:17 AM, Jordan Spivack
<jordan@athenssocialmedia.com> wrote:
> Hey everyone, sorry this is a repost from Stack Overflow but I still
> desperately need a definitive answer.
>
> I'm currently using PHP, the mdb2 package, MySQL, and the InnoDB table
> engine for database development. I want to be 100% sure that when I insert
> into a table and then use lastInsertID(), it will contain the correct auto
> increment value for that table regardless of whether or not another user is
> executing the same query. Basically, I need to lock the tables.
>
> So for a standard series of 3 statements:
>
> 1. Insert into table A
> 2. $id = $mdb2->lastInsertID()
> 3. Insert into table B (name, fk) VALUES ('foo', $id)
>
> what steps do I need to take to make sure $id has the value of the insert
> from step 1? Is it fine the way it is? Does everything need to be in a
> transaction? Do I need to add other queries to lock and release the tables?
>
> Thanks guys.
>
>
> --
> Jordan Spivack
> Lead Web Architect - Athens Social Media
> cell: 706.207.4974
> http://www.athenssocialmedia.com
>
hi.
from the documentation:
http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodlastInsertID
"Returns the autoincrement ID if supported or $id or fetches the
current ID in a sequence called: $table.(empty($field) ? '' :
'_'.$field)"
checking the source code reveals that both mysql and mysqli driver
uses the 'SELECT LAST_INSERT_ID()' for getting the last insert id
http://svn.php.net/viewvc/pear/packages/MDB2/trunk/MDB2/Driver/mysql.php?view=markup#l1325
http://svn.php.net/viewvc/pear/packages/MDB2/trunk/MDB2/Driver/mysqli.php?view=markup#l1334
there can be some gotchas with some mysql versions and edge cases, but
it should do the job:
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_last-insert-id
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu