php get ID from database code example
Example 1: get id of record created php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Example 2: mysql get this inserted id php
$this_id = mysqli_insert_id($db);
Example 3: get id php
<a href="index.php?id=<?php echo $my_id;?>&name=<?php echo $my_name;?>Click</a>
<?php
$id = intval($_GET['id']);
$name = strval($_GET['name']);
?>