What potential issues can arise when trying to retrieve the last inserted ID in a non-persistent database connection?

When trying to retrieve the last inserted ID in a non-persistent database connection, the issue that can arise is that the connection may be closed before you can retrieve the ID. To solve this, you should retrieve the last inserted ID immediately after the insert query is executed.

// Execute insert query
$query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";
$result = mysqli_query($connection, $query);

// Retrieve last inserted ID
$last_id = mysqli_insert_id($connection);

echo "Last inserted ID: " . $last_id;