php pdo multiple insert

Solutions on MaxInterview for php pdo multiple insert by the best coders in the world

showing results for - "php pdo multiple insert"
Helena
23 Oct 2018
1function placeholders($text, $count=0, $separator=","){
2    $result = array();
3    if($count > 0){
4        for($x=0; $x<$count; $x++){
5            $result[] = $text;
6        }
7    }
8
9    return implode($separator, $result);
10}
11
12$pdo->beginTransaction(); // also helps speed up your inserts.
13$insert_values = array();
14foreach($data as $d){
15    $question_marks[] = '('  . placeholders('?', sizeof($d)) . ')';
16    $insert_values = array_merge($insert_values, array_values($d));
17}
18
19$sql = "INSERT INTO table (" . implode(",", $datafields ) . ") VALUES " .
20       implode(',', $question_marks);
21
22$stmt = $pdo->prepare ($sql);
23$stmt->execute($insert_values);
24$pdo->commit();
25