PHP mysqlinsertid Manual. If you insert a data row by using the ON DUPLICATE KEY UPDATE clause in an INSERT statement, the mysqlinsertid function will return not the same results as if you directly use LASTINSERTID in My. SQL. See the following example lt insert a datarow, primary key is autoincrement value is a unique keyquery INSERT INTO test value VALUES test mysqlquery query echo LASTINSERTID ,mysqlquery SELECT LASTINSERTID ,lt br mysqlinsertid ,mysqlinsertid This will print LASTINSERTID 1mysqlinsertid 1. In this case the function returns the same as the My. SQL Statement. But see the insert on an existing key lt query INSERT INTO test value VALUES testON DUPLICATE KEY UPDATE value test. LASTINSERTID ,mysqlquery SELECT LASTINSERTID ,lt br mysqlinsertid ,mysqlinsertid This will print LASTINSERTID 2mysqlinsertid 1. By using the ON DUPLICATE KEY UPDATE clause, only the old datarow will be modified, if the INSERT statement causes a duplicate entry, but the LASTINSERTID function returns the next autoincrement value for the primary key, which is by the way not set as the next autoincrement value in the database. The mysqlinsertid function returns the primary key of the old and changed data row. For me this is the right operation method, because the LASTINSERTID function returns a value which is not referenced to a data row at all. Greets from Munich. The fastest MySQL solution, without inner queries and without GROUP BY SELECT m. FROM topten m. The MySQLi Extension MySQL Improved is a relational database driver used in the PHP scripting language to provide an interface with MySQL databases. QA for database professionals who wish to improve their database skills and learn from others in the community.