Working in PHP and using MYSQLI. Trying to get the insert_id for all rows inserted from a single insert query with multiple values. Keeping it in a single call for efficiency. My actual code has hundreds of values in one insert query. However, here is some sample code for an insert with just 4 values:
$sql = "INSERT INTO link_tags ( string_names_id, urls_id )
VALUES ( '2', '17' ), ( '8', '31' ), ( '8', '1' ), ( '8', '4' )";
$mysqli->query($sql);
echo "id's: " .$mysqli->insert_id;
The above code achieves the insert, but only gives me the id of the first insert from the call. How can I retrieve all of the ID's for this single query?
As @thomas pointed, I used this solution and suppose it's ok to detect last insert id;
In your situation (for all ids);
Meanwhile, I used
multi_query
and handled result in while loop but could not achieve expected result.It should be safe to assume that your ids are mysql_insert_id + the next 3 (whatever) in succession, as your statement is done in one transaction
No, this isn't possible with MySQL.
Its not even really save to use
LAST_INSERT_ID()
as it will return a BIGINT (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column.You could guess that this value + length of your data list will be the ID's but there isn't any absolute guarantee this will always be true.