Get auto-generated id in the last query in PHP

Description

The following code shows how to get auto-generated id in the last query.

Example


/*from   w ww.j  av  a  2s . c om*/
<?php
    $con=mysqli_connect("localhost","root","","test");
    // Check connection
    if (mysqli_connect_errno()){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    mysqli_query($con,"INSERT INTO employee (Name) VALUES ('XML')");

    // Print auto-generated id
    echo "New record has id: " . mysqli_insert_id($con);

    mysqli_close($con);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented