Last active
April 5, 2021 11:41
-
-
Save cp6/69f5713fe0c9208411c1c968d7addd90 to your computer and use it in GitHub Desktop.
PHP PDO insert from a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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