note 59445 added to function.mysql-insert-id

From: Date: Wed, 07 Dec 2005 05:53:28 +0000
Subject: note 59445 added to function.mysql-insert-id
Groups: php.notes 
Request: Send a blank email to php-notes+get-99668@lists.php.net to get a copy of this message
As mentioned by frumler at the-beach dot no_spam dot net the LAST_INSERT_ID works like a charm when inserting values into tables. I'm not sure why anyone would need mysql_insert_id() when LAST_INSERT_ID is readily available. example: Say you have a table called "transaction" and a table called "accounts". Obviously each account must be created using a transaction, so every time a record is created in the accounts table, a record must be created in the transaction table containing the same account_id(auto_increment) that was just created by mysql. Here's a simple way to do this: <?php $account_query="INSERT INTO accounts (account_id,type) VALUES (NULL,'saving')"; $transaction_query="INSERT INTO transactions(transaction_id,account_id) VALUES (NULL,LAST_INSERT_ID)"; $a_query=mysql_query($account_query); $t_query=mysql_query($transaction_query); ?> ---- Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59445 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59445&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59445&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59445&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59445&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59445&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59445&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59445&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59445&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99668) next »