PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | Php vs Node.Js | Web Tools

Get Last ID after Insert

Tutorials > Get Last ID after Insert

You can get the ID (AUTO_INCREMENT column as primary key) generated after you insert a new record in your table by a simple function "mysql_insert_id()".

This tutorial require 1 PHP file and 1 table of mySQL database.

  1. get_last_id.php
  2. Database "tutorial" and table "name_list" with 2 fields: id(auto_increment), name(varchar, 50).


Source Code
// Connect database.

$name="John Brown";

// Insert record
mysql_query("insert into name_list(name) values('$name')");

// Get the last insert record and put it to $last_id;

echo "The last record is : ";
echo $name;
echo "<br>";

echo "The last record ID is : ";
echo $last_id;

Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
Introduction & Get Start
Using Form
PHP Tags
PHP Variables
PHP Control Structures
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record
[email protected]