6

Is there any efficient way to retrieve all last inserted rows auto incremented IDs from mysql database using php? Suppose I don't know how many rows are there to be inserted in a batch but I need all of their IDs after insertion.

After Googling I didn't find any efficient way to do this, so I'm not sure is that possible. I can follow this one but have to hit database multiple times to retrieve IDs.

$data = array (
array(1,2),
array(3,4),
array(5,6)
);

$ids = array();

foreach ($data as $item) {
   $sql = 'insert into table (a,b) values ('.$item[0].','.$item[1].')';
   mysql_query ($sql);
   $ids[] = mysql_insert_id();
}

Thank You

leevi
  • 91
  • 1
  • 7

5 Answers5

1

your code is near complete solution.

$data = array (
array(1,2),
array(3,4),
array(5,6)
);

$ids = array();

foreach ($data as $item) {
   $sql = "INSERT INTO `table` (`a`, `b`) VALUES ('{$item[0]}' ,'{$item[1]}');";
   mysql_query ($sql) or mysql_error();
   $ids[] = mysql_insert_id();
}

var_dump($ids);

If the question is about performance issue, then another solution could be

foreach ($data as $item) {
   $sql = "INSERT INTO `table`.`table` (`a`, `b`) VALUES ('{$item[0]}' ,'{$item[1]}');";
   mysql_query ($sql) or mysql_error();
}

$last_id = mysql_insert_id();
$total = count($data);
for($i = 0; $i <= $total; $i++){
    $ids[] = $total-$i;
}

This will prevent multiple sql query.

1

Mysql only returns the last inserted id, unfortunatelly there's no way to retrieve all of them (Directly). You will have to use work arounds, for example:

  1. Calculate the inserted rows, and the last inserted ID, and calculate the rest backward.

  2. Generate custom ID's instead of letting mysql create them incrementally, and if the insert query success, store the id's into an array (Normally if one of them fails, the whole query would fail anyway).

The second method is recommended, because with n1, if you are doing slighly complex operations, for example using INSERT blah blah ON DUPLICATE KEY the modifies would count as 2 inserts (don't ask me why) instead of one, and you have to do some calculations to see how many were actually inserted and how many modified.

By the way, stop using mysql_ extension, it's deprecated, use mysqli or pdo instead

aleation
  • 4,796
  • 1
  • 21
  • 35
  • Thanks for your response. I'm thinking of an efficient solution in terms of auto increment actually. FYI: I'm aware of the mysql_ extensions and using mysqli_ already in my actual code. Thanks :) – leevi Apr 10 '13 at 07:38
  • One idea would be to add a datetime or timestamp to the inserts, get the last inserted id's datetime, and query all the id's with that datetime. – aleation Apr 10 '13 at 07:40
0

If your auto_incremented field is "normal", so it does not behave in some special way but increases the last id by one, you can find the maximal value of it before insertion and then find all the records that are higher of it.

Example:

$sql = "SELECT id FROM table ORDER BY id DESC LIMIT 1";
    // get maximal value before insert

$sql = "SELECT id FROM table WHERE id > :last_id"; // get all new

But this solution is not much professional.

Voitcus
  • 4,463
  • 4
  • 24
  • 40
  • 2
    Will be inaccurate if anything else makes a request that inserts into the db - which would be the case if the same page/request was loaded by two users - if that's not a factor, may work. However, in that case why not simply run the first query - twice. `range($firstMax, $lastMax)` - would give all ids. And will work even if the range is a huge number. – AD7six Apr 10 '13 at 07:23
  • 1
    i would run the first query after the first insert. if you do that befor the first insert you run into huge problems if someone deleted some of the last rows and the autoincrement != max(id)!! – ITroubs Apr 10 '13 at 07:27
  • @AD7six Yes, you are right. I haven't thought about the possibility of inserting rows from another source. – Voitcus Apr 10 '13 at 07:27
0

When i need to track batches of records entered, I have an extra field in the table, usually labeled as stamp or something. When the records are inserted, the stamp field is then populated with the current timestamp as given by the time() function. Then every batch can be tracked, no matter how old they are.Also, saving the timestamp is an easy way to get the exact time when they were added.

-1

Getting the id of last insert id

    <?php

$conn = mysql_connect('localhost', 'user', 'password');

mysql_select_db('test');

$data = array (
array(1,2),
array(3,4),
array(5,6)
);

$ids = array();

foreach ($data as $item) {

   $sql = 'insert into testtable (a,b) values ('.$item[0].','.$item[1].')';   
   mysql_query ($sql, $conn);
   $id[] = mysql_insert_id($conn);
}
  • 1
    -1 This looks like a copy of the code in the question - with a typo in it – AD7six Apr 10 '13 at 07:37
  • @AD7six:I have used the same code but modify mysql_insert_id($conn) instead of mysql_insert_id. As I am new to stackoverflow, so I thought, it would be nice to share the complete example. If you know, how to share this type of example, please let me know. – Jayendra Kainthola Apr 10 '13 at 07:44
  • *[If the link identifier is not specified, the last link opened by mysql_connect() is assumed](http://php.net/manual/en/function.mysql-insert-id.php)* - i.e. adding `$conn` to the code doesn't change how it works. Changing `$ids` to `$id` just means a new variable has all the ids instead of the one the OP is expecting. – AD7six Apr 10 '13 at 07:54
  • OK. But it only work for me when I added $conn into mysql_insert_db. Thanks for providing link, I will go through to this. – Jayendra Kainthola Apr 10 '13 at 07:58