note 59445 deleted from function.mysql-insert-id by cmb
| From: | cmb@php.net | Date: | Sun, 16 Feb 2020 13:41:29 +0000 |
| Subject: | note 59445 deleted from function.mysql-insert-id by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214129@lists.php.net to get a copy of this message | ||
Note Submitter: Soroushl at gmail dot com
----
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);
?>