Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $data = array();
- for ($i = 0; $i < 100; $i++)
- {
- $name = md5(rand(0, 1000));
- $price = rand(0, 1000);
- $data[] = array('name' => $name, 'price' => $price);
- }
- function placeholders($text, $count=0, $separator=",")
- {
- $result = array();
- if($count > 0)
- {
- for($x=0; $x<$count; $x++)
- {
- $result[] = $text;
- }
- }
- return implode($separator, $result);
- }
- $db->beginTransaction();
- $insert_values = array();
- $datafields = array('name', 'price');
- $question_marks = array();
- foreach($data as $d)
- {
- $question_marks[] = '(' . placeholders('?', sizeof($d)) . ')';
- $insert_values = array_merge($insert_values, array_values($d));
- }
- $sql = "INSERT INTO products (" . implode(",", array_values($datafields) ) . ") VALUES " . implode(',', $question_marks);
- $stmt = $db->prepare($sql);
- try
- {
- $stmt->execute($insert_values);
- }
- catch (PDOException $e)
- {
- out($e->getMessage());
- }
- $db->commit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement