获取插入MySQL数据库的最后一条记录的id [重复]

This question already has an answer here:

I'm trying to get the id of the last record inserted into a MySQL database using MAX(id). I can't figure out why my query is not returning any results. Is there something wrong with my PHP?Tthe query works if I try it inside phpmyadmin.

include("db_conx.php"); //Connect to db mysqli
$sql = "SELECT MAX(id) FROM tbl_uploads";  
$result = $db_conx->query($sql);
$row = $result->fetch_assoc();
echo 'last_id: '.$row['id'];
</div>

use an alias for get the value

  $sql = "SELECT MAX(id) as max_id FROM tbl_uploads";  
  $result = $db_conx->query($sql);
  $row = $result->fetch_assoc();
  echo 'last_id: '.$row['max_id'];

you can use this built in function for get last insert id

$last_id = mysqli_insert_id($conn);

if you want to get max record id from your db and if id this your primary key then you can use this code

$sql = "SELECT id FROM tbl_uploads order by id desc";  
$result = $db_conx->query($sql);
$row = $result->fetch_assoc();
echo 'last_id: '.$row['id'];

and if you want to get specific max record then use this code. if id your filed then you can use this code . you use alias for get record

$sql = "SELECT MAX(id) as max_id FROM tbl_uploads";  
$result = $db_conx->query($sql);
$row = $result->fetch_assoc();
echo 'last_id: '.$row['max_id'];

Use MAX(id) as id in select query

include("db_conx.php"); //Connect to db mysqli
$sql = "SELECT MAX(id) as id FROM tbl_uploads";  
$result = $db_conx->query($sql);
$row = $result->fetch_assoc();
echo 'last_id: '.$row['id'];

Try with this :

$sql = "SELECT * FRON tbl_uploads ORDER BY id DESC LIMIT 1";

It will output your id.