Question

Lets imagine situation where we should insert a lot of rows in the loop using PDO.

$sql = "INSERT INTO products (name, price) VALUES (:name, :price)";
$stmt = $db->prepare($sql);

for ($i = 0; $i < 100; $i++)
{
    $name = md5(rand(0, 1000));
    $price = rand(0, 1000);

    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':price', $price);

    try
    {
        $result = $stmt->execute();

        if (!$result)
        {

            print_r($db->errorInfo());
        }

        echo $db->lastInsertId();
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }
}

In this way all 100 rows will not be inserted to the database. And echo at 23th line will output something like:

1 2 3 4 5 ... 59 60 61 61 61 61 61 61

and print_r at 20th line will output

 Array (
     [0] => 00000
     [1] => 
     [2] =>  
 )

PDO error code 00000 means that everything works fine. And no rows were affected. And if I try to manually insert row on which $result was false - all is ok.

and only 61 rows will be inserted to the table. And each time script is running this number is changing and this is really strange.

Why?

In other way - we could make one query from all insert queries and there are all 100 rows will be inserted. Here is a link to pastebin with code.

Here is a table structure:

CREATE TABLE IF NOT EXISTS `products` (
`id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `price` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

btw. I use percona MySQL server (5.5) with HandlerSocker plugin. And I tried to insert rows using HandlerSocket. Here is a code

$hs = new \HSPHP\WriteSocket();
$hs->connect();

$id = $hs->getIndexId('test','products','','name,price');
$loops_number = 10000;

for ($i = 0; $i < $loops_number; $i++)
{
    $name = 'handler-'.md5(rand(0, 1000));
    $price = rand(0, 1000);

    $hs->insert($id, array($name, $price));
}

And I after this - I have ~ 14000 rows in DB. Why? Also if I change number of loops (variable $loops_number) -

if 10 loops - I have 100 rows in DB table
if 50 loops - 50 rows
if 100 loops - 100 rows
if 500 loops - 500 rows
if 1000 loops - ~1100 rows and this number always change. (if I truncate table and run script again)

Seems problem with my MySQL server?

Was it helpful?

Solution 2

I resolve this problem by reinstalling PHP. (I used PHP from this repo: ppa:ondrej/php5)

And problem with HandlerSocket inserting: 1. create index inside the loop 2. if you make insert query by HandlerSocket better to set id (auto increment) manually

OTHER TIPS

Whoa!!! That is a lot of info to look at bro. Am not sure whether i have got your question right but if I have then the below piece of code will achieve the same effect only better:

$name = md5(rand(0, 1000));
$price = rand(0, 1000);

try{

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    for($i=0, $i<100, $i++){

        $sql = "INSERT INTO products (name, price) VALUES (:name, :price)";
        $stmt = $db->prepare($sql);
        $stmt->execute(array(':name'=>$name[$i], ':price'=>$price[$i]));

        echo $db->lastInsertId();

    }

}catch(PDOException $e){


    print_r($db->errorInfo());

    echo 'An error occured'.$e->getMessage();

} 

Give it a try and tell me what you got.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top