Php methods for obtaining the largest database id

Source: Internet
Author: User
Today, when I was in a database category, I was unable to obtain the ID of the last saved data. after repeated troubleshooting, I still did not find the cause, however, I summarized the IDs of the last record in the specified mysql table or the IDs of the latest inserted records. let's take a look at them. Development... today, when I was in a database category, I was unable to obtain the ID of the last saved data. after repeated troubleshooting, I still did not find the cause, however, I summarized the IDs of the last record in the specified mysql table or the IDs of the latest inserted records. let's take a look at them.

I like development best. Writing a program can improve your strength and add some code to your favorites! It is really good. For some reason, you need to obtain the maximum id of the database. So this section of php obtains the largest id code of the database. Here, max (id). here, the id is used to obtain the largest id. For other fields, enter other fields.

Method 1

$ Fh = mysql_query ("select MAX (id) from log"); $ c_echo = mysql_fetch_array ($ fh); echo $ c_echo;

Method 2

 

The PHP function mysql_insert_id () returns the value of the field defined by AUTO_INCREMENT after the last INSERT query is executed.

Method 3

Msyql_query ("select last_insert_id ()");

Last_insert_id () is a mysql function that also takes effect on the current link.

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


Tutorial address:

Reprinted! But please include the article address ^

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.