2

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 ?

Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57

2 Answers2

4

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.

Wikunia
  • 1,564
  • 1
  • 16
  • 37
0

Try this:

Procedural style of coding:

<?php
$host = ''; 
$user = '';
$password = '';
$database = '';


$link = mysqli_connect($host, $user, $password, $database);

if(!$link)
{
    echo('Unable to connect to the database!');
}
        ELSE {
           $sql = "INSERT IGNORE INTO `table_name` (`field1`,`field2`) VALUES ('val1','val2'), ('val3','val4')"; 
           $result = mysqli_query($link, $sql);
                    echo mysqli_affected_rows($link);
                }

        mysqli_close($link);
?>

mysqli_affeccted_rows counts the number of inserts. I think that @wikunia's answer will probably yield the same result. I was in the process of answering you question, before wikunia beat me to it. I place it anyway.

Mr. Radical
  • 1,847
  • 1
  • 19
  • 29