Skip to content

Instantly share code, notes, and snippets.

@terrymun
Last active August 29, 2015 14:27
Show Gist options
  • Save terrymun/32ffc0a49982782397e7 to your computer and use it in GitHub Desktop.
Save terrymun/32ffc0a49982782397e7 to your computer and use it in GitHub Desktop.
Executing multiple prepared statements
<?php
// Assuming that database connection is already open
// Receive user input that is separated by a new line for each row
$favouriteFoods = array('Apple', 'Poutine', 'Cheesestrings', 'Pickle chips', 'Peanut butter and raisins');
$user = 'johndoe';
// Prepare statement
$stmt = $db->prepare("INSERT INTO users (user, foodItem) VALUES(:username, :favouriteFood)");
$db->beginTransaction();
// Insert a new row for each food item
foreach($favouriteFoods as $food) {
$db->execute(array(':username'=>$user, ':favouriteFood'=>$food));
}
$db->commit();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment