PHP mysqli_insert_id()没有INSERT的函数

时间:2015-01-16 18:57:18

标签: php sql mysqli

PHP mysqli_insert_id()函数

此代码打印w3school.com中的最后一个ID:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 
VALUES ('Glenn','Quagmire',33)");

// Print auto-generated id
echo "New record has id: " . mysqli_insert_id($con); 

mysqli_close($con);
?>

但我不想插入任何内容......我只是想拥有最后一个ID,

我尝试构建我自己的函数:

function getlast()
    {
        $con = connectDB();
        $sql = "SELECT id FROM Articls";
        $result = $con->query($sql);
        $latestID = $result->insert_id;
        disconnectDB($con);
        return $latestID;

    }

但似乎错了

1 个答案:

答案 0 :(得分:3)

只需选择MAX ID -

即可
 $sql = "SELECT MAX(id) FROM Articls";

MySQL MAX