Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mllvzeth/be5dce2bee914c423bd0b7ffca68da00 to your computer and use it in GitHub Desktop.
Save mllvzeth/be5dce2bee914c423bd0b7ffca68da00 to your computer and use it in GitHub Desktop.
simplified automatic PDO INSERT SQL time saver ⌛ by @Rami_jamleh; with revision by @Clair_Shaw
<?php
/**
* @Rami jamleh - http://php.net/manual/en/pdostatement.execute.php#111823
* @Clair_Shaw(Revision) - http://php.net/manual/en/pdostatement.execute.php#116901
* simplified $placeholder form
**/
$data = ['a'=>'foo','b'=>'bar'];
$keys = array_keys($data);
$fields = '`'.implode('`, `',$keys).'`';
//$placeholder = substr(str_repeat('?,',count($keys),0,-1));
$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