Alireza Fallah Alireza Fallah - 3 months ago 18
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 ?

Answer

The answer is affected_rows

$db = new mysqli('127.0.0.1','...','...','...');
$sql = "INSERT IGNORE INTO Test (id,test) VALUES (1,2),(1,3),(2,2),(3,4)";
$ins_test = $db->prepare($sql);
$ins_test->execute();
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.

Comments