PHP 3 ways to get MySQL new record ID value

Source: Internet
Author: User
Tags php programming

From:http://www.jb51.net/article/51473.htm

This article mainly introduces PHP to get the MySQL new record ID value of 3 methods, generally use PHP with function mysql_insert_id () can be implemented, the other 2 ways to use the right for special cases, the need for friends can refer to the next

First, use the statement:

Copy CodeThe code is as follows: mysql_query ("SELECT Max (ID) from T1", $link);


Using this method gives the highest ID value, which is the last value, but when a multilink thread, this maximum ID is not necessarily the self-increment ID value of the inserted data, and therefore does not apply to multithreading.

Second, use the function: msyql_insert_id ();

In PHP, it is often necessary to remove the ID value from the inserted database, and there is exactly one function:

Copy CodeThe code is as follows:
<?php
Execute the statement that inserts the database
......
$getID =mysql_insert_id ();//$getID is the last record of the id===> by myself to verify, in the newer PHP version, to use $getId = mysqli_insert_id ($link);

Conditions of use for this function:
1. Assume that the field name is RecordID
2. Field properties must be set to: auto_increment
3. Use after adding data
$newID = mysql_insert_id ();
Get ID Value
?>

The PHP function, mysql_insert_id (), returns the value of the field defined by Auto_increment after the last insert query was executed.

When the system executes the INSERT, and then executes the SELECT, it may have been distributed to a different back-end server, and if you are using PHP programming, you should use MYSQL_INSERT_ID () to get the latest inserted ID, each time the insert ends, In fact, the corresponding AutoIncrement value has been calculated to return to PHP, you do not have to issue a separate query, directly with the mysql_insert_id ().
When a statement is inserted, it automatically returns the last ID (MySQL self-increment).
And this function is only useful for the current link, that is, it is multi-user-safe.
It is recommended to use this function;
Problem: When the ID is bigint, it does not work.

Third, use the query

Copy CodeThe code is as follows: Msyql_query ("Select last_insert_id ()");


LAST_INSERT_ID () is a MySQL function that is also valid for the current link
This usage resolves the bigint type problem encountered in mysql_insert_id ()
Summarize:
It is recommended to use method two, in special cases, consider method three.

PHP 3 ways to get MySQL new record ID value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.