Home  >  Article  >  Backend Development  >  PHP gets the ID value of the data just inserted_PHP tutorial

PHP gets the ID value of the data just inserted_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:261440browse

This article will introduce to you the method of PHP to obtain the ID value of the data just inserted. PHP provides the mysql_insert_id() function. We only need to add echo mysql_insert_id() after inserting the data.

Example

The code is as follows
 代码如下 复制代码

//执行插入数据库的语句
//……
$getID=mysql_insert_id();//$getID即为最后一条记录的ID
?>

Copy code

//Execute the statement inserted into the database

代码如下 复制代码

msyql_query("select last_insert_id()");

//……

$getID=mysql_insert_id();//$getID is the ID of the last record
?>

The PHP function mysql_insert_id() returns the value of the field defined by AUTO_INCREMENT after the last INSERT query was executed.

There is another way
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn