2015-04-07 69 views
0

獲取mysql數據庫中最後一條記錄的「id」的值讓我們假設有一個名爲「db」的數據庫和一個名爲「tb」的表,並在其中包含一些記錄。第一列是「自動遞增」的「ID」。我想最後一條記錄的ID到一個PHP頁面(我已經創建了連接到數據庫)如何通過php

$conn = new mysqli($servername, $username, $password, $dbname); 
+0

'SELECT MAX(ID)FROM tb'或'SELECT ID結核病ORDER BY ID DESC LIMIT 1'或... – Sean

+0

http://php.net/manual/en/ mysqli.insert-id.php – sinisake

+0

ORDER BY id ASC; – Ghostman

回答

0

您正在尋找這樣的事情:

mysqli_query("SELECT `ID` FROM `table` ORDER BY ID DESC limit 1"); 
1
Select id from tb ORDER BY id desc limit 1 
+0

'DESC'不是'ASC',因爲OP聲明'我想得到最後一條記錄的ID' – Sean

+0

更新後檢查答案 – Ghostman

0

這裏的完整的解決方案:

<?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; 
?>