Вопрос

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

Это было полезно?

Решение

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.

Другие советы

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

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.

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.

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);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top