如何使用PHP获取Mysql数据库中的最后一个字段?

时间:2022-01-26 06:59:44

I have mysql database and I want to get the last ID from field in a table .

我有mysql数据库,我想从表中的字段中获取最后的ID。

example : -

例子:-

id   Value
1    david
2    jone
3    chris

I want a function return 3 if this table exist .

如果这个表存在,我想要一个函数返回3。

5 个解决方案

#1


9  

If you want to select the ID of the most recently inserted row in a table with an AUTO_INCREMENT column, you will likey be interested in MySQL's LAST_INSERT_ID function.

如果您想在具有AUTO_INCREMENT列的表中选择最近插入的行的ID,您将对MySQL的LAST_INSERT_ID函数感兴趣。

#2


15  

You can use:

您可以使用:

SELECT MAX(id) FROM table

#3


9  

SELECT id
FROM table
ORDER BY id DESC
LIMIT 1

#4


-1  

you can also use the following code ::

您也可以使用以下代码:

 $lastId = mysql_insert_id();

#5


-2  

SELECT * FROM table ORDER BY id DESC LIMIT 1

#1


9  

If you want to select the ID of the most recently inserted row in a table with an AUTO_INCREMENT column, you will likey be interested in MySQL's LAST_INSERT_ID function.

如果您想在具有AUTO_INCREMENT列的表中选择最近插入的行的ID,您将对MySQL的LAST_INSERT_ID函数感兴趣。

#2


15  

You can use:

您可以使用:

SELECT MAX(id) FROM table

#3


9  

SELECT id
FROM table
ORDER BY id DESC
LIMIT 1

#4


-1  

you can also use the following code ::

您也可以使用以下代码:

 $lastId = mysql_insert_id();

#5


-2  

SELECT * FROM table ORDER BY id DESC LIMIT 1