Code has been added to clipboard!

MySQL Last Insert ID for MySQLi Procedural

Example
<?php
  $host = 'host';
  $user = 'user';
  $pass = 'pass';
  $db = 'db';
  // Create connection  
  $conn = mysqli_connect($host, $user, $host, $db);  	
  // Check connection  	
  if (!$conn) { 
    die("Failed to connect: " . mysqli_connect_error());
  }
  $sql = "INSERT INTO users (username, password, email) VALUES ('Johny', 'Dawkins', '[email protected]')";  	
  if (mysqli_query($conn, $sql)) { 
    $latest_id =  mysqli_insert_id($conn);    
    echo "Insert successful. Latest ID is: " . $latest_id;
  } else { 
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  }
  mysqli_close($conn);  
?>