| Viewed 99 times

How to get last inserted id in PHP


How to get last inserted id in PHP

PHP
1 Answer
Mohammad Qandeel
Answered
8

When a successful attempt to Insert  item`s  to the database  
You can get AUTO_INCREMENT number through mysqli_insert_id() function

mysqli_insert_id() Return integer number

Example 

<?php
    $link = mysql_connect('localhost', 'userName', 'password');
    mysql_select_db('My_DB');
    mysql_query( "INSERT INTO `users` (`email`)values ('me@example.com')" );
    echo mysql_insert_id();
?>

this will print the ID generated for an  AUTO_INCREMENT column by the last query in the users table