Description
PDOModel insert batch function provides a simplest way to insert the batch of data into database by passing the data in form of associative array.
You need to pass table name and array of array of data to insert function and rest will be managed by the script.
Function Defination
void function insertBatch($dbTableName, $insertBatchData);
Parameters Details
| Details | Type | Example |
|---|---|---|
| $dbTableName - Table name to insert records | string | Any table name like "orderTable", "wp_posts" etc |
| $insertBatchData - Array of Associative array of records to be inserted, with columnName as key and columnValue as value | assoc. array | array(array("firstName" => "John", "lastName" => "Jonathan"), array("firstName" => "Simon", "lastName" => "Kane")) |
Examples
$pdomodel = new PDOModel(); //create object of the PDOModel class
$pdomodel->connect("localhost", "root", "", "pdocrud");//connect to database
/* Insert function */
$pdomodel->insertBatch("emp", array(array("firstName" => "John", "lastName" => "Jonathan"), array("firstName" => "Simon", "lastName" => "Kane")));
More Examples
//Example 2
$insertData = array(array("firstName" => "Johnq", "lastName" => "Jonathan"), array("firstName" => "Michal", "lastName" => "Kane"));
$pdomodel->insertBatch("emp", $insertData);
//Example 3
$insertEmpData[0]["firstName"] = "Nike";
$insertEmpData[0]["lastName"] = "jason";
$insertEmpData[0]["age"] = 25;
$insertEmpData[1]["firstName"] = "Rasaol";
$insertEmpData[1]["lastName"] = "jason";
$insertEmpData[1]["age"] = 25;
$pdomodel->insertBatch("emp", $insertEmpData);
Result/Output
Record will be inserted in database
Debug
| Option | Details | Example |
|---|---|---|
| $pdomodel->getLastQuery(); | Return last query executed | INSERT INTO `emp` (`firstName`,`lastName`,`age`) VALUES (?,?,?) |
| print_r($pdomodel->error) | Print errors (if any) | |
| $pdomodel->lastInsertId | Return last insert Id | 53 |
| $pdomodel->rowsChanged; | Return Rows created/changed | 6 |