Skip to content

Instantly share code, notes, and snippets.

@ansulev
Last active July 7, 2018 07:20
Show Gist options
  • Save ansulev/33d2279336ab7b09ae9f869fcaf378c5 to your computer and use it in GitHub Desktop.
Save ansulev/33d2279336ab7b09ae9f869fcaf378c5 to your computer and use it in GitHub Desktop.
PDOStatement:execute - simplified $placeholder form
<?php
/*
*from
* http://php.net/manual/en/pdostatement.execute.php#111823
* simplified $placeholder form
*/
$data = ['a'=>'foo','b'=>'bar'];
$keys = array_keys($data);
$fields = '`'.implode('`, `',$keys).'`';
#here is my way
$placeholder = substr(str_repeat('?,',count($keys)),0,-1);
$pdo->prepare("INSERT INTO `baz`($fields) VALUES($placeholder)")->execute(array_values($data));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment