Get the last ID when inserting into the MySQL database

0

How to get the last ID when inserting in MySQL BD using PHP and MYSQLi.

$inserir = $conexao->query("INSERT INTO cadastro (nome, sobrenome) VALUE (
          'Fulano',
          'De tall'
");

echo $id = mysqli_insert_id($conexao);
  

You are returning 0.

    
asked by anonymous 23.07.2017 / 22:38

1 answer

1

The problem has already been fixed, but I would add a useful tip. You can make errors in sql queries show as php exceptions, allowing faster debugging. This can be done by using the mysqli_report function. Example:

$driver_mysqli = new mysqli_driver();
$driver_mysqli->report_mode = MYSQLI_REPORT_ALL;

$inserir = $conexao->query("INSERT INTO cadastro (nome, sobrenome) VALUE (
          'Fulano',
          'De tall'
");

echo $id = mysqli_insert_id($conexao);
    
24.07.2017 / 00:57