Skip to content

Instantly share code, notes, and snippets.

@cp6
Last active April 5, 2021 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cp6/69f5713fe0c9208411c1c968d7addd90 to your computer and use it in GitHub Desktop.
Save cp6/69f5713fe0c9208411c1c968d7addd90 to your computer and use it in GitHub Desktop.
PHP PDO insert from a loop
<?php
$db = new PDO("mysql:host=127.0.0.1;dbname=test;charset=utf8mb4", 'root', '');
$db->beginTransaction();
$insert = $db->prepare("INSERT IGNORE INTO `objects` (`id`, `color`) VALUES (?, ?);");
$test_array = ['Red', 'Orange', 'Pink', 'Lime', 'Yellow', 'Gold', 'Green', 'Blue', 'Purple', 'Maroon', 'Silver', 'Aqua'];
$id = 0;
foreach ($test_array as $c) {
$id++;
$insert->execute([$id, $c]);
}
$db->commit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment