PHP PDO - doesnt insert all statements in the loop -


lets imagine situation should insert lot of rows in loop using pdo.

$sql = "insert products (name, price) values (:name, :price)"; $stmt = $db->prepare($sql);  ($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 way 100 rows not inserted database. , echo @ 23th line output like:

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

and print_r @ 20th line output

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

pdo error code 00000 means works fine. , no rows affected. , if try manually insert row on $result false - ok.

and 61 rows inserted table. , each time script running number changing , really strange.

why?

in other way - make 1 query insert queries , there 100 rows inserted. here link pastebin code.

here 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. use percona mysql server (5.5) handlersocker plugin. , tried insert rows using handlersocket. here code

$hs = new \hsphp\writesocket(); $hs->connect();  $id = $hs->getindexid('test','products','','name,price'); $loops_number = 10000;  ($i = 0; $i < $loops_number; $i++) {     $name = 'handler-'.md5(rand(0, 1000));     $price = rand(0, 1000);      $hs->insert($id, array($name, $price)); } 

and after - have ~ 14000 rows in db. why? if change number of loops (variable $loops_number) -

if 10 loops - 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 , number change. (if truncate table , run script again) 

seems problem mysql server?

whoa!!! lot of info @ bro. not sure whether have got question right if have below piece of code achieve same effect 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 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 try , tell me got.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -