[ create a new paste ] login | about

Link: http://codepad.org/1EflAjgn    [ raw code | output | fork ]

PHP, pasted on Dec 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$array = array(
    array('name', 'age', 'gender' ),
    array('Ian', 24, 'male'),
    array('Janice', 21, 'female')
);

$fields = implode(', ', array_shift($array));

$values = array();
foreach ($array as $rowValues) {
    foreach ($rowValues as $key => $rowValue) {
         $rowValues[$key] = $rowValues[$key];
    }

    $values[] = "(" . implode(', ', $rowValues) . ")";
}

$query = "INSERT INTO table_name ($fields) VALUES " . implode (', ', $values);
echo $query;


Output:
1
INSERT INTO table_name (name, age, gender) VALUES (Ian, 24, male), (Janice, 21, female)


Create a new paste based on this one


Comments: