Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

php pdo insert batch multiple rows with placeholders using -'php,insert,pdo,bulkinsert'

php pdo insert batch multiple rows with placeholders  using -'php,insert,pdo,bulkinsert'

I am looking to do multiple inserts using PHP PDO.

The closest answer I have found is this one


However the example thats been given uses ?? instead of real placeholders.

I have looked at the examples on the PHP doc site for place holders pdo.prepared-statements

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

Now lets say I wanted to achieve the above but with an array

$valuesToInsert = array(
  0 => array('name' => 'Robert', 'value' => 'some value'),
  1 => array('name' -> 'Louise', 'value' => 'another value')

How would I go about it with PDO and multiple inserts per transaction?

I imagine it would start of with a loop?

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");

foreach($valuesToInsert as $insertRow){

    // now loop through each inner array to match binded values
    foreach($insertRow as $column => value){
        $stmt->bindParam(":{$column}", value);

However the above does not work but hopefully will demonstrate what im trying to achieve

asked Sep 8, 2015 by rajesh
0 votes

Related Hot Questions

Government Jobs Opening