PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE table_name ( id INT AUTO_INCREMENT, column_1 INT, column_2 INT, PRIMARY KEY (id) );
Copy Clear
Copy Format Clear
<?php $data_list = [ ['specific_key' => 1, 'another_key' => 2], ['specific_key' => 3, 'another_key' => 4], ['specific_key' => 5, 'another_key' => 6], ]; /*$values = []; $placeholders = []; foreach ($data_list as $row) { $placeholders[] = '(?,?)'; array_push($values, $row['specific_key'], $row['another_key']); } $statement = $pdo->prepare( sprintf( 'INSERT INTO `table_name` (`column_1`,`column_2`) VALUES %s', implode(',', $placeholders) ) ); $statement->execute($values); */ $values = implode(",", array_fill(0, count($data_list), "(?, ?)")); $statement = $pdo->prepare( "INSERT INTO `table_name`( `column_1`, `column_2` ) VALUES {$values}" ); $counter = 0; foreach ($data_list as $data) { $statement->bindValue(++$counter, $data['specific_key'], PDO::PARAM_INT); $statement->bindValue(++$counter, $data['another_key'], PDO::PARAM_INT); } $statement->execute(); var_export( $pdo->query("SELECT * FROM table_name")->fetchAll(PDO::FETCH_ASSOC) );
Show:  
Copy Clear