mysqli_insert_id()不工作?

时间:2011-04-13 23:29:48

标签: sql

我对此感到非常生气,我不知道这种简单的情况怎么会这么复杂而且无法奏效。

基本上:

$lastInsertId = mysqli_insert_id($query);
echo $lastInsertId;

返回NULL

我的$查询运行正常,插入完美。任何人都知道为什么我没有得到最后一个插入ID?

2 个答案:

答案 0 :(得分:3)

什么是$query?您必须将mysqli链接作为参数($link = mysqli_connect(...))传递。也许你的$ query是mysqli_query或其他的结果。

mixed mysqli::mysqli_insert_id ( mysqli $link )

手动:http://php.net/mysqli_insert_id

答案 1 :(得分:0)

//you follow the folow the bellow code
<?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//
 //NOTE: $con link should be in ()
 echo "New record has id: " . mysqli_insert_id($con); 

 mysqli_close($con);
  ?>