PDO Insert batch Function insert database operation using GOModel


GOModel 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"))


                                $GOModel = new GOModel(); //create object of the GOModel class
                                $GOModel->connect("localhost", "root", "", "database");//connect to database
                                /* Insert function */
                                $GOModel->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"));
                                $GOModel->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;
                                $GOModel->insertBatch("emp", $insertEmpData);


Record will be inserted in database


Option Details Example
$GOModel->getLastQuery(); Return last query executed INSERT INTO `emp` (`firstName`,`lastName`,`age`) VALUES (?,?,?)
print_r($GOModel->error) Print errors (if any)
$GOModel->lastInsertId Return last insert Id 53
$GOModel->rowsChanged; Return Rows created/changed 6