mysqli_insert_id() not working?
I'm getting quite angry over this, I don't know how can this simple situation be so complicated and not work.
Basically:
$lastInsertId = mysqli_insert_id($query);
echo $lastInsertId;
returns NULL
my $query runs fine, inserts perfectly. Anyone knows why I'm 开发者_运维问答not getting the last insert id?
What is $query
? You have to pass the mysqli link as parameter ($link = mysqli_connect(...)
). Maybe your $query is the result of mysqli_query or something else.
mixed mysqli::mysqli_insert_id ( mysqli $link )
Manual: http://php.net/mysqli_insert_id
//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);
?>
精彩评论