Skip to content
Advertisement

Get insert_id for all rows inserted in single mysqli query (multiple values)

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement