Code has been added to clipboard!
MySQL Last Insert ID for MySQLi Object-Oriented
Example
<?php
$host = 'host';
$user = 'user';
$pass = 'pass';
$db = 'db';
// Create connection
$conn = new mysqli($host, $user, $pass, $db);
// Check connection
if ($conn->connect_error)Â { Â Â Â
die("Failed to connect: " . $conn->connect_error);
}
$sql = "INSERT INTO users (username, password, email) VALUES ('Johny', 'Dawkins', '[email protected]')";
if ($conn->query($sql) === TRUE) {
$latest_id = $conn->insert_id;Â Â Â
echo "Insert successful. Latest ID is: " . $latest_id;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>