I am executing multiple SQL insert commands and one select query in a single request to the database server but I get back a result for each command as well.
Simplified example:
$query = 'INSERT INTO table1 (column1) VALUES (123);' .
'SET @id = LAST_INSERT_ID();' .
'INSERT INTO table2 (column1, column2) VALUES (@id, 456);' .
'INSERT INTO table3 (column1, column2) VALUES (@id, 789);' .
'SELECT @id';
$conn = new mysqli('server', 'user', 'pass', 'name');
if ($conn->multi_query($query)) {
do {
if ($result = $conn->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
} while ($conn->next_result());
}
This results in three empty results and the fourth is the one I want.
Is it possible to not return results for the non-queries (the INSERTs and SET) and only get one result (for the SELECT) so I don't have to go through each to get what I want?
NB. Not wanting to use a stored procedure for this due to the complexity and variability of the dynamically generated INSERTs