php-获取最后一个插入ID

时间:2016-06-15 10:11:28

标签: php mysql

我知道这个问题已被多次回答。我从这里尝试了一些解决方案,但没有任何效果。我没有使用任何PHP框架。

我有一个插入操作,我想得到插入行的id。

这是我的代码:

$qry="INSERT INTO tablename(content) VALUES('".$content."')";
setData($qry);

setData()是一个用户定义的函数,它执行插入操作。

//for data submit
function setData($qry)
{
    $obj=new DBCon();
    $res=$obj->submitQuery($qry);
    return $res;
}

//fetches result
function getData($qry)
{
    $obj=new DBCon();
    $res=$obj->selectQuery($qry);
    return $res;
}

这是我为建立数据库连接所做的课程:

class DBCon
    {   
        private function getConnection()
        {   
                               // hostname,   username,     password,   database
            $con=mysqli_connect("localhost","root","","dbname")OR die('Could not Connect the Database');        
            return $con;
        }

        public function closeCon()
        {
            mysqli_close($con);
        }

        public function submitQuery($qry)
        {
            $result=0;
            $con=$this->getConnection();
            $result=mysqli_query($con,$qry);
            return $result;
        }

        public function selectQuery($qry)
        {
            $con=$this->getConnection();
            $res=mysqli_query($con,$qry);
            return $res;
        }
    }

要获取最后插入的id,我编写了以下查询,但它没有产生任何结果:

SELECT LAST_INSERT_ID() FROM tablename

还有其他方法可以解决这个问题吗?

4 个答案:

答案 0 :(得分:2)

LAST_INSERT_ID()仅返回MySQL服务器为AUTO_INCREMENT列自动生成的值;插入特定值时,不会发生自动生成,LAST_INSERT_ID().

也不会返回任何值

尝试:$con->insert_id;

答案 1 :(得分:1)

尝试这种方式。

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('First Name', 'Last Name', 'first@example.com')";

if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id;
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

答案 2 :(得分:1)

MySQLi面向对象

 <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id;
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

MySQLi程序

     <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?> 

PDO

 <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";
    // use exec() because no results are returned
    $conn->exec($sql);
    $last_id = $conn->lastInsertId();
    echo "New record created successfully. Last inserted ID is: " . $last_id;
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?> 

答案 3 :(得分:0)

 $last_id = $conn->insert_id;