1

Is there a way to bind array params in bindParam function of pdo without using foreach statement? (something like mysql.connector in python).
the foreach version would be like this:

$data = array('name'=>'something','job'=>'something else');
foreach($data as $key => $value){
    $stmt->bindParam(':'.$key, $value);
}
mehrdadep
  • 972
  • 1
  • 15
  • 37
  • 2
    You can bind multiple parameters via array in the pdo execute: https://stackoverflow.com/questions/12344741/binding-multiple-values-in-pdo – FMK Mar 12 '18 at 16:33

1 Answers1

4

Many PDO users think they have to use bindParam(). You don't.

You can pass an array directly to execute() with all your parameter values. It's this easy:

$stmt->execute($data);

If you used named parameters in your SQL, use a hash array. If you used positional parameters, use a plain array.

For more complete code examples, read them here: http://php.net/manual/en/pdo.prepare.php

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828