0

let's say there is a database called "db" and a table called "tb" and with some records in it. first column is "ID" which is "auto increment". i want to get the last record's ID to a php page.(i have already created the connection to the db)

$conn = new mysqli($servername, $username, $password, $dbname);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
DScript
  • 285
  • 2
  • 8
  • `SELECT MAX(ID) FROM tb` or `SELECT ID FROM tb ORDER BY ID DESC LIMIT 1` or ... – Sean Apr 07 '15 at 17:19
  • http://php.net/manual/en/mysqli.insert-id.php – sinisake Apr 07 '15 at 17:19
  • possible duplicate of [SQL Select only rows with Max Value on a Column](http://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column) – psadac Apr 07 '15 at 17:48

3 Answers3

1
Select id from tb ORDER BY id desc limit 1
Ghostman
  • 6,042
  • 9
  • 34
  • 53
0

Are you looking for something like this:

mysqli_query("SELECT `ID` FROM `table` ORDER BY ID DESC limit 1");
Keep Coding
  • 636
  • 9
  • 26
0

Here's the complete solution:

<?php 
$conn = new mysqli($servername, $username, $password, $dbname);
$rs = mysqli_query("SELECT MAX(id) as max_id FROM tb");
$res = mysql_fetch_object($rs);
echo $id = $res->max_id;
?>
Raj Ankur
  • 114
  • 6