Best way to find last inserted id in mysql using php

I am wondering what the best solution is to get the last inserted id after mysql query?

I found the following solutions:

<?php function get_current_insert_id($table) { $q = "SELECT LAST_INSERT_ID() FROM $table"; return mysql_num_rows(mysql_query($q)) + 1; } ?> 

or even with the mysql_insert_id php mysql_insert_id , but apparently this function will not work well with bigint (this is what I use for the ID field), and if there are a lot of consecutive sql queries, it can be unreliable.

Can someone provide a reliable and quick solution to solve this problem?

+5
source share
2 answers

If you insert multiple rows with the same INSERT statement, LAST_INSERT_ID () returns the value generated only for the first inserted row. The reason for this is to make it easy to play the same INSERT statement against any other server.

+4
source

All Articles