PHP obtains the MySQL incremental ID

Source: Internet
Author: User
This article mainly solves the following problems: PHP obtains the incremental ID of a MySQL DATA table (PHPgetautoincrementIDvalue); PHP obtains the ID of the last MySQL record; PHP obtains the incremental ID of a MySQL table, obtain the attributes of the mysql data table and obtain AUTO_INCREMENT. $ Table_nameTable1; $ querymysql_query (SH

This article mainly solves the following problems: PHP obtains the incremental ID (PHP get auto increment ID value) of the MySQL DATA table; PHP obtains the ID of the last MySQL record; PHP obtains the incremental ID of the MySQL table. method 1, obtain the attributes of the mysql data table and obtain AUTO_INCREMENT. $ Table_name = Table1; $ query = mysql_query (SH

This article mainly solves the following problems:

  • PHP obtains the incremental ID (PHP get auto increment ID value) of the MySQL DATA table );
  • PHP obtains the ID of the last MySQL record;
PHP obtains the incremental ID of a MySQL table.

Method 1: Obtain the attributes of the mysql data table and obtain AUTO_INCREMENT.

$ Table_name = "Table1 ";
$ Query = mysql_query ("show table status where name = '$ table_name '");
$ Row = mysql_fetch_array ($ query );
$ Next_inc_value = $ row ["AUTO_INCREMENT"];

This SQL statement was mentioned in a previous article about MySQL commonly used practical SQL statements. It can get the attribute value of the entire table.

The incremental ID obtained in this way will continue to increase even if one ID is deleted, and will not be repeated unless manually set. For example, you only need to set AUTO_INCREMENT to 10000 if you want to start the order number from 10000.

Method 2: Get the Maximum System ID and Add 1 to get the incremental ID.

$ Result = mysql_query ("select max (id) as max FROM table ");
$ Got = mysql_fetch_array ($ result );
$ Next_id = $ got ['Max'] + 1;

It does not completely get the next auto-increment value. Only increase the largest ID by 1, but it is the largest ID value and will not be repeated.

PHP obtains the ID of the last MySQL record.

Method 1: Use the PHP function mysql_insert_id () to obtain the ID of the last inserted record in MYSQL. If the column type of AUTO_INCREMENT is BIGINT, the value returned by mysql_insert_id () is incorrect.

(...)
Read the rest of PHP to get the MySQL incremental ID (9 words)

©Lixiphp for LixiPHP, 2013. | Permalink | No comment | Add to del. icio. us
Post tags: AUTO_INCREMENT, MySQL, PHP, incremental ID

Feed enhanced by Better Feed from Ozh

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.