Last active
July 7, 2018 07:20
-
-
Save ansulev/33d2279336ab7b09ae9f869fcaf378c5 to your computer and use it in GitHub Desktop.
PDOStatement:execute - simplified $placeholder form
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 | |
/* | |
*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