Advertisement
Guest User

Untitled

a guest
Jul 31st, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. $data = array();
  2.  
  3. for ($i = 0; $i < 100; $i++)
  4. {
  5.  
  6.     $name = md5(rand(0, 1000));
  7.     $price = rand(0, 1000);
  8.  
  9.  
  10.     $data[] = array('name' => $name, 'price' => $price);
  11. }
  12.  
  13. function placeholders($text, $count=0, $separator=",")
  14. {
  15.     $result = array();
  16.  
  17.     if($count > 0)
  18.     {
  19.         for($x=0; $x<$count; $x++)
  20.         {
  21.             $result[] = $text;
  22.         }
  23.     }
  24.  
  25.     return implode($separator, $result);
  26. }
  27.  
  28. $db->beginTransaction();
  29.  
  30. $insert_values = array();
  31. $datafields = array('name', 'price');
  32. $question_marks = array();
  33.  
  34. foreach($data as $d)
  35. {
  36.  
  37.     $question_marks[] = '('  . placeholders('?', sizeof($d)) . ')';
  38.     $insert_values = array_merge($insert_values, array_values($d));
  39. }
  40.  
  41. $sql = "INSERT INTO products (" . implode(",", array_values($datafields) ) . ") VALUES " . implode(',', $question_marks);
  42.  
  43.  
  44. $stmt = $db->prepare($sql);
  45.  
  46. try
  47. {
  48.     $stmt->execute($insert_values);
  49. }
  50. catch (PDOException $e)
  51. {
  52.  
  53.     out($e->getMessage());
  54.  
  55. }
  56.  
  57. $db->commit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement