note 102464 deleted from function.mysql-insert-id by danbrown

From: Date: Wed, 16 Feb 2011 15:10:02 +0000
Subject: note 102464 deleted from function.mysql-insert-id by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177034@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- 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']; } ?>

« previous php.notes (#177034) next »