last_insert_id.php
<?php
//Config
$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";
//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);
//Check Connection
if (!$connection) {
die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
echo "Connection Successfull <br>";
}
//SQL Command
$sql_command = "INSERT INTO people (id, name, lastname, telephon, email, address)
VALUES (NULL, 'John', 'Smith', 555777, 'mailx@server.net', 'Main Road 12a')";
//Check SQL Command
if (mysqli_query($connection, $sql_command)) {
$last_entry = mysqli_insert_id($connection);
echo "SQL Command OK, Last ID: " . $last_entry . "<hr>" ;
} else {
echo "SQL ERROR" . mysqli_error($connection);
}
?>
After we run ti multiple times:
Connection Successfull
SQL Command OK, Last ID: 11
No comments:
Post a Comment