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).


get_last_id.php

Source Code
<?
// Connect database.
mysql_connect("localhost","","");
mysql_select_db("tutorial");

$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;
$last_id=mysql_insert_id();

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

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

Advertise
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
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

PHPsimple.net
info@phpsimple.net