PHP/MySQL insert row then get 'id'

前端 未结 10 1026
臣服心动
臣服心动 2020-11-22 16:03

The \'id\' field of my table auto increases when I insert a row. I want to insert a row and then get that ID.

I would do it just as I said it, but is there a way I c

10条回答
  •  爱一瞬间的悲伤
    2020-11-22 16:38

    Another possible answer will be:

    When you define the table, with the columns and data it'll have. The column id can have the property AUTO_INCREMENT.

    By this method, you don't have to worry about the id, it'll be made automatically.

    For example (taken from w3schools )

    CREATE TABLE Persons
    (
    ID int NOT NULL AUTO_INCREMENT,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    PRIMARY KEY (ID)
    )
    

    Hope this will be helpful for someone.

    Edit: This is only the part where you define how to generate an automatic ID, to obtain it after created, the previous answers before are right.

提交回复
热议问题