Wednesday, April 23, 2025

MySQLi Last Insert ID

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

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...