First, the use of statements:
Copy Code code as follows:
mysql_query ("SELECT Max (ID) from T1", $link);
Using this method, the maximum ID value is the last value, but when a multilink connection is used, the maximum ID is not necessarily the ID value of the data we insert, so it does not apply to multithreading.
two, use function: msyql_insert_id ();
In PHP, it is often necessary to remove the ID value from the Insert database, and there is just one function:
Copy Code code as follows:
<?php
Executing statements that insert a database
......
$getID =mysql_insert_id ();//$getID is the ID of the last record
Conditions used for this function:
1. Suppose the field name is RecordID
2. The field property must be set to: auto_increment
3. Use after adding data
$newID = mysql_insert_id ();
Get ID Value
?>
PHP function mysql_insert_id () is the value of the field defined by Auto_increment after the last time an insert query was executed.
When the system completes the insert, and then executes the SELECT, it may have been distributed to different back-end servers, and if you are programming with PHP, you should get the newly inserted ID through mysql_insert_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 mysql_insert_id () can be.
When you insert a statement, 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 that this function be used;
Problem: When ID is bigint type, it is not working.
Third, the use of inquiries
Copy Code code as follows:
Msyql_query ("Select last_insert_id ()");
LAST_INSERT_ID () is a MySQL function is also valid for the current link
This usage solves the bigint type problem encountered in mysql_insert_id ()
Summarize:
Recommended use of method two, under special circumstances, you can consider method three.