PHP mysql_insert_id () function gets the last Insert record ID number

Source: Internet
Author: User

This problem has been encountered by the cliff before, too long did not write do not remember (seems to be the CI frame directly related functions), and then met again, rolled to check again, here is not the case is PDO, but with the outdated connection and implementation of how to operate after the way.

What SQL statement is implemented, but obviously inappropriate, when the number of people to operate, suddenly chaos.

So here, with the mysql_insert_id () function, he will return the auto_incresement value.

The code is as follows Copy Code

<?php
$link = mysql_connect (' localhost ', ' mysql_user ', ' Mysql_password ');
if (! $link) {
Die (' Could not connect: '. Mysql_error ());
}
mysql_select_db (' mydb ');

mysql_query ("INSERT into mytable (product) VALUES (' Kossu ')");
printf ("Last inserted record has ID%d", mysql_insert_id ());
?>

At first I still think this function will not encounter the same problem, and then Nova told me is based on the current database connection, immediately not afraid of it.

Of course there are.

The code is as follows Copy Code

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

Now, of course, this connection is out of the way,


There are also instructions here:

Warning

This extension is deprecated as of PHP 5.5.0, and would be removed in the future. Instead, the mysqli or pdo_mysql extension should be used. Also mysql:choosing an API guide and related FAQs For more information. Alternatives to this function include:

mysqli_insert_id ()
Pdo::lastinsertid ()

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.