note 102464 added to function.mysql-insert-id
| From: | Anonymous at osu1 dot php dot net | Date: | Wed, 16 Feb 2011 15:08:58 +0000 |
| Subject: | note 102464 added to function.mysql-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177033@lists.php.net to get a copy of this message | ||
I just wrote a function, which returns the last inserted auto-incrementing id:
<?php
$r = mysql_query("INSERT INTO
my_table (...) VALUES (...)");
$id = last_insert_id( $r );
if( false === $id) echo 'error';
function last_insert_id_after_single_insert( $r ){
if( true !== $r) return false;
if( 1 !== mysql_affected_rows()) return false;
$r = mysql_query("SELECT LAST_INSERT_ID() AS id"); // INSERT returns
true/false
if( 1 !== mysql_num_rows( $r )) return false; // false or other num
$r = mysql_fetch_array( $r );
if( false === $r) return false;
return $r['id'];
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 212.40.99.52
----
Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php
Edit -- https://master.php.net/note/edit/102464
Del: integrated -- https://master.php.net/note/delete/102464/integrated
Del: useless -- https://master.php.net/note/delete/102464/useless
Del: bad code -- https://master.php.net/note/delete/102464/bad+code
Del: spam -- https://master.php.net/note/delete/102464/spam
Del: non-english -- https://master.php.net/note/delete/102464/non-english
Del: in docs -- https://master.php.net/note/delete/102464/in+docs
Del: other reasons-- https://master.php.net/note/delete/102464
Reject -- https://master.php.net/note/reject/102464
Search -- https://master.php.net/manage/user-notes.php