PHP How to get MySQL new record ID value

Source: Internet
Author: User
Tags mysql version

The ID of the new record found today with mysql_insert_id () is incorrect,

Although the source code is found to have a transaction write problem, but the most fundamental reason is that I inserted the data ID type is bigint type

The method to get MySQL new record ID value is

1. Using statements

mysql_query ("SELECT Max (ID) from user", $link);

2. Use the function msyql_insert_id ();

(1) MySQL version

int mysql_insert_id ([resource $link _identifier = NULL])

Returns the value of the field defined by Auto_increment after the last INSERT query was executed.

Returns the ID generated by the INSERT operation in the previous step. If the previous query did not produce an ID of auto_increment, then mysql_insert_id () returns 0

But when the ID is bigint, it's not working.

(2) Version mysqli

Mixed mysqli_insert_id (mysqli $link)

You can return an int or a string based on the size of the result

3. Use the query

Msyql_query ("Select last_insert_id ()");

LAST_INSERT_ID () is a MySQL function

This method solves the problem of bigint type encountered in mysql_insert_id ().

LAST_INSERT_ID () returns the ID of the auto_increment.

If the return is 0, the auto_increment is not set in the view table structure, or the function of insert delay is not used (the immediate return ID value is not returned)

Functions in the 4.PDO

public string Pdo::lastinsertid ([string $name = NULL])

Returns the ID or sequence value of the last inserted row

  

 

PHP How to get MySQL new record ID value

Related Article

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.