Grab the last ID when inserting into Mysql database

Asked

Viewed 800 times

0

How to get the last ID when inserting into BD Mysql, using PHP and Mysqli.

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

echo $id = mysqli_insert_id($conexao);

Is returning 0.

  • 1

    Looks to me like we’re missing one ) in his insert on declension value. See if the insert is working.

  • @True cat. I put the ) and it worked. Thank you.

  • Worse than this fault does not accuse error.

1 answer

2

The problem has already been solved, but I would add a useful tip. You can cause errors in sql queries to be shown as php exceptions, allowing for faster debugging. This can be done 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.