Get Last ID after Insert
In many cases it will be very usefull if we get the auto increment primary key value, that is generated after any mysql insertion statement. The sample php code given below will help us to get the same. Happy coding
<?
// Connect database.
mysql_connect("localhost","username","password");
mysql_select_db("database");
$site="http://www.samplephpcodes.com";
// Insert record to your table
mysql_query("insert into websites(site) values('$site')");
// Get the last insert record and put it to $last_id;
$last_id=mysql_insert_id();
echo "you have inserted : ";
echo $site;
echo "<br>";
echo "The last record ID is : ";
echo $last_id;
echo '<a href="http://samplephpcodes.com">Sample PHP Codes</a>';
?>
Home
See the related postsPHP Warning: Call-time pass-by-reference has been deprecated Working with Database (MySQL) Cookies in PHP Session Management Functions in php Join array elements with a string – implode |




