note 83550 added to function.mysql-insert-id
| From: | dane514ATgmailDOTCOM at osu1 dot php dot net | Date: | Sat, 31 May 2008 04:05:47 +0000 |
| Subject: | note 83550 added to function.mysql-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140025@lists.php.net to get a copy of this message | ||
I may have solved the problems that a number of you have brought to everyone's attention. Here
is a simple function that will always return the correct ID of the afffecting row (i.e., the row
that will soon be inserted into)--no matter how many consecutive users are online and
updating/inserting into the same table that you are also inserting/updating.
Retrieving the ID of the row soon-to-be-affected was imperative to my current project, and the
various COUNT() solutions just did not cut it.
Use this before any insert/update statement to retrieve the ID of the row to be inserted into:
<?php
function get_current_insert_id($table)
{
$q = "SELECT LAST_INSERT_ID() FROM $table";
return mysql_num_rows(mysql_query($q)) + 1;
}
?>
It would be great to read any discussions if anyone considers this function problematic.
----
Server IP: 216.235.15.211
Probable Submitter: 216.252.79.107
----
Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php
Edit -- https://master.php.net/note/edit/83550
Del: integrated -- https://master.php.net/note/delete/83550/integrated
Del: useless -- https://master.php.net/note/delete/83550/useless
Del: bad code -- https://master.php.net/note/delete/83550/bad+code
Del: spam -- https://master.php.net/note/delete/83550/spam
Del: non-english -- https://master.php.net/note/delete/83550/non-english
Del: in docs -- https://master.php.net/note/delete/83550/in+docs
Del: other reasons-- https://master.php.net/note/delete/83550
Reject -- https://master.php.net/note/reject/83550
Search -- https://master.php.net/manage/user-notes.php