PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_insert_id() function in PHP


Tutorials »Php »

Topic

What is mysql_insert_id() function in PHP?
How does mysql_insert_id() works?




Explanation

The mysql_insert_id() function returns the AUTO_INCREMENT ID generated from the previous INSERT operation.

Syntax
int mysql_insert_id ( [resource link_identifier])

Returns ID generated for an AUTO_INCREMENT column by the previous INSERT query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.

mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier is not specified, the last opened link is assumed for connection.

Example


<?php

//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");

//select database
mysql_select_db('database',$link);

$query = "insert into my_table values('','Bill',2000,'Germany')";

//execute query
$result = mysql_query($query);

//print AUTO_INCREMENT ID generated from the previous INSERT operation
printf("Last inserted record has id :%d\n", mysql_insert_id());

?>



RESULT:

Last inserted record has id : 10

See also: mysql_query().





Other Links

web hosting