phpmysqlmysqliprepared-statement

Using Prepared Statement, how I return the id of the inserted row?


I want retrieve the id of a inserted row in the database, but I don't know how to do this.

I tried to return using the SQL clause RETURNING id, but not works.

How I can return the id after the insertion of a row?


Solution

  • After calling the execute() method on the prepared statement, the id of the insert row will be accessible via the insert_id attribute.

    $pstm->execute();
    $pstm->insert_id;
    

    Or using procedural style:

    mysqli_stmt_execute($pstm);
    mysqli_stmt_insert_id($pstm);