Alireza Fallah Alireza Fallah - 2 months ago 11x
MySQL Question

INSERT IGNORE INTO - Number of rows inserted

I'm going to insert about 500 records in a table using one query :

$sql = "INSERT IGNORE INTO `table_name` (`field1`,`field2`)
VALUES ('val1','val2') ('val3','val4') ... ";
// php_mysql_insert_function

How can I find out haw many rows are inserted in after executing query ?


The answer is affected_rows

$db = new mysqli('','...','...','...');
$sql = "INSERT IGNORE INTO Test (id,test) VALUES (1,2),(1,3),(2,2),(3,4)";
$ins_test = $db->prepare($sql);
echo $db->affected_rows;

In this example Test has 2 columns id and test (both integer) and id is the primary key. The table is empty before this insert.

The programm echos 3.