PDO Insert Batch Operation

Description

Insert batch of new records in a table using array of associative array. It creates prepared statement only once in case of PDO and Mysqli so it is much faster as compared to inserting entries one by one. Instead of writing long insert queries, you needs to pass array of keys(columns) and values(insert values). This function will automatically create query for you and inserts data.

Function Reference :
void dbInsertBatch(string $table_name, assoc. array $insert_batch_array); 

Parameters

Name Type Required Parameter Info Example
table_name string Yes The name of the table to insert new records any table name like employee_table,user_table
insert_batch_array associative array Yes Associative array with key as column name and values as column value. array(array("role_name"=>"Admin","role_description"=>"Administrator"), array("role_name"=>"Manager","role_description"=>"Manages users"))

Examples

<?php 
 $sdb= new SimplifiedDB(); 
 $sdb->dbConnect("localhost","username","password","dbuser"); 
 $sdb->dbInsertBatch("user_roles",
 array(array("role_name"=>"Admin","role_description"=>"Administrator"),
 array("role_name"=>"Manager","role_description"=>"Manages users")));
 ?> 

Result

New rows will be inserted in table.

Debuging

Debug Operation Result/Output
$sdb->query INSERT INTO user_roles (`role_name`,`role_description`) values (?,?)
$sdb->last_insert_id 6
$sdb->rows_affected 1
$sdb->message_info Connected to database
$sdb->error_info

More Examples

 
            $insertBatchRoles=array();
            $insertBatchRoles[]=array("role_name"=>"Admin","role_description"=>"Administrator"); 
            $insertBatchRoles[]=array("role_name"=>"Manager","role_description"=>"Manages users");
            $sdb->dbInsertBatch("user_roles",$insertBatchRoles);