Search code examples
phpmysqllastinsertidlast-insert-id

how to get array of latest auto incremented id's?


One single Query to insert multiple rows :

$My query = "INSERT INTO exp_channel_titles(channel_id,author_id,ip_address) VALUES(11,1,'203.109.119.2'),(11,1,'203.109.119.2'),(11,1,'203.109.119.2')";

What I want :

array of auto incremented id.

what i am getting using SELECT LAST_INSERT_ID() :

I get only one single id.What should I do so that I get array of all the auto incremented value of last inserted fields.

P.S : I cant fire multiple insert query.


Solution

  • This gets last X records ordered by ID descreasingly. If your ID is autoincremented, those will be the latest inserted values.

    SELECT * FROM exp_channel_titles
    ORDER BY id DESC
    LIMIT X