To find the next (still not used) auto-increment, I am using this function for somewhat years now.
public function getNewAI($table)
{
$newAI = false;
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SHOW TABLE STATUS LIKE '".$table."'";
$result = $mysqli->query($sql);
if($result) {
$row = $result->fetch_assoc();
$newAI = $row['Auto_increment'];
}
$mysqli->close();
return $newAI;
}