mysqlsqlphpmyadminlastinsertidlast-insert-id

PHPMYADMIN LAST_INSERT_ID returns 0


I have problem with LAST_INSERT_ID.

CREATE TABLE for_test(
id_test INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
test_name VARCHAR(30)
);

INSERT INTO for_test (test_name) VALUES ('test1');
INSERT INTO for_test (test_name) VALUES ('test2');

I added successfully two records - yet SELECT LAST_INSERT_ID() still returns 0. (I use InnoDB if that even matters)

Could anyone tell me what I'm doing wrong?


Solution

  • No other query after insert and they both should execute together. Be careful that. Also if you can not handle it you can use

    SELECT id FROM for_test ORDER BY id DESC LIMIT 1;
    

    and you can read this manual

    How to use LAST_INSERT_ID()