Search code examples
databasemysqlimysql-insert-id

does mysqli_insert_id only work if I have a column called ID?


Hello I was wondering that if I use the mysqli_insert_id function will this only insert an auto increment id value if I have a column called ID because at the moment I have one called UserID and I was wondering if this would cause problems for me?


Solution

  • The column doesn't have to be called ID in order for mysqli_insert_id to be useful, but you do have to have an AUTO_INCREMENT column, and i'm fairly certain it has to be the primary key.

    The way you're describing it, though, it sounds like you might be misunderstanding what mysqli_insert_id does. It doesn't insert an ID; it returns the last auto-generated ID it assigned. So you use it after you've inserted a row, if you want to use that ID to add other records that refer to the row you just inserted.