Code has been added to clipboard!
MySQL Last Insert ID for MySQLi Procedural
Example
<?php
$host = 'host';
$user = 'user';
$pass = 'pass';
$db = 'db';
// Create connection
$conn = mysqli_connect($host, $user, $host, $db);
// Check connection
if (!$conn)Â {
die("Failed to connect: " . mysqli_connect_error());
}
$sql = "INSERT INTO users (username, password, email) VALUES ('Johny', 'Dawkins', '[email protected]')";
if (mysqli_query($conn, $sql)) {Â
$latest_id = mysqli_insert_id($conn);Â Â Â
echo "Insert successful. Latest ID is: " . $latest_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>