public function insertBatch($table, $data = [], $safeModeInsert = true)
    {

        // PDO transactions start
        $this->start();
        // check if table name not empty
        if (!empty($table)) {
            // and array data not empty
            if (count($data) > 0 && is_array($data)) {
                // get array insert data in temp array
                $tmp = [];
                foreach ($data[0] as $f => $v) {
                    $tmp[] = ":s_$f";
                }
                // make name space param for pdo insert statement
                $sNameSpaceParam = implode(', ', $tmp);
                // unset temp var
                unset($tmp);
                // get insert fields name
                $sFields = implode(', ', array_keys($data[0]));
                // handle safe mode. If it is set as false means user not using bind param in pdo
                if (!$safeModeInsert) {
                    // set pdo insert statement in class property
                    $this->sql = "INSERT INTO `$table` ($sFields) VALUES ";
                    foreach ($data as $key => $value) {
                        $this->sql .= '(' . "'" . implode("', '", array_values($value)) . "'" . '), ';
                    }
                    $this->sql = rtrim($this->sql, ', ');
                    // return this object
                    // return $this;
                    // pdo prepare statement
                    $this->STH = $this->prepare($this->sql);
                    // start try catch block
                    try {
                        // execute pdo statement
                        if ($this->STH->execute()) {
                            // store all last insert id in array
                            $this->allLastId[] = $this->lastInsertId();
                        } else {
                            self::error($this->STH->errorInfo());
                        }
                    } catch (\PDOException $e) {
                        // get pdo error and pass on error method
                        self::error($e->getMessage() . ': ' . __LINE__);
                        // PDO Rollback
                        $this->back();
                    }// end try catch block

                    // PDO Commit
                    $this->end();
                    // close pdo
                    $this->STH->closeCursor();
                    // return this object
                    return $this;
                }

                // end here safe mode
                // set pdo insert statement in class property
                $this->sql = "INSERT INTO `$table` ($sFields) VALUES ($sNameSpaceParam);";
                // pdo prepare statement
                $this->STH = $this->prepare($this->sql);
                // set class property with array
                $this->data = $data;
                // set batch insert flag true
                $this->batch = true;
                // parse batch array data
                foreach ($data as $key => $value) {
                    // bind pdo param
                    $this->_bindPdoNameSpace($value);

                    try {
                        // execute pdo statement
                        if ($this->STH->execute()) {
                            // set class property with last insert id as array
                            $this->allLastId[] = $this->lastInsertId();
                        } else {
                            self::error($this->STH->errorInfo());
                            // on error PDO Rollback
                            $this->back();
                        }
                    } catch (\PDOException $e) {
                        // get pdo error and pass on error method
                        self::error($e->getMessage() . ': ' . __LINE__);
                        // on error PDO Rollback
                        $this->back();
                    }
                }
                // fine now PDO Commit
                $this->end();
                // close pdo
                $this->STH->closeCursor();
                // return this object
                return $this;
            } else {
                self::error('Data not in valid format..');
            }
        } else {
            self::error('Table name not found..');
        }
    }
public function insertBatch($table, $data = [], $safeModeInsert = true)
    {

        // PDO transactions start
        $this->start();
        // check if table name not empty
        if (!empty($table)) {
            // and array data not empty
            if (count($data) > 0 && is_array($data)) {
                // get array insert data in temp array
                $tmp = [];
                foreach ($data[0] as $f => $v) {
                    $tmp[] = ":s_$f";
                }
                // make name space param for pdo insert statement
                $sNameSpaceParam = implode(', ', $tmp);
                // unset temp var
                unset($tmp);
                // get insert fields name
                $sFields = implode(', ', array_keys($data[0]));
                // handle safe mode. If it is set as false means user not using bind param in pdo
                if (!$safeModeInsert) {
                    // set pdo insert statement in class property
                    $this->sql = "INSERT INTO `$table` ($sFields) VALUES ";
                    foreach ($data as $key => $value) {
                        $this->sql .= '(' . "'" . implode("', '", array_values($value)) . "'" . '), ';
                    }
                    $this->sql = rtrim($this->sql, ', ');
                    // return this object
                    // return $this;
                    // pdo prepare statement
                    $this->STH = $this->prepare($this->sql);
                    // start try catch block
                    try {
                        // execute pdo statement
                        if ($this->STH->execute()) {
                            // store all last insert id in array
                            $this->allLastId[] = $this->lastInsertId();
                        } else {
                            self::error($this->STH->errorInfo());
                        }
                    } catch (\PDOException $e) {
                        // get pdo error and pass on error method
                        self::error($e->getMessage() . ': ' . __LINE__);
                        // PDO Rollback
                        $this->back();
                    }// end try catch block

                    // PDO Commit
                    $this->end();
                    // close pdo
                    $this->STH->closeCursor();
                    // return this object
                    return $this;
                }

                // end here safe mode
                // set pdo insert statement in class property
                $this->sql = "INSERT INTO `$table` ($sFields) VALUES ($sNameSpaceParam);";
                // pdo prepare statement
                $this->STH = $this->prepare($this->sql);
                // set class property with array
                $this->data = $data;
                // set batch insert flag true
                $this->batch = true;
                // parse batch array data
                foreach ($data as $key => $value) {
                    // bind pdo param
                    $this->_bindPdoNameSpace($value);

                    try {
                        // execute pdo statement
                        if ($this->STH->execute()) {
                            // set class property with last insert id as array
                            $this->allLastId[] = $this->lastInsertId();
                        } else {
                            self::error($this->STH->errorInfo());
                            // on error PDO Rollback
                            $this->back();
                        }
                    } catch (\PDOException $e) {
                        // get pdo error and pass on error method
                        self::error($e->getMessage() . ': ' . __LINE__);
                        // on error PDO Rollback
                        $this->back();
                    }
                }
                // fine now PDO Commit
                $this->end();
                // close pdo
                $this->STH->closeCursor();
                // return this object
                return $this;
            } else {
                self::error('Data not in valid format..');
            }
        } else {
            self::error('Table name not found..');
        }
    }
public function insertBatch ( $ table , $ data = [ ] , $ safeModeInsert = true ) { // PDO transactions start $ this -> start ( ) ; // check if table name not empty if ( ! empty ( $ table ) ) { // and array data not empty if ( count ( $ data ) > 0 && is_array ( $ data ) ) { // get array insert data in temp array $ tmp = [ ] ; foreach ( $ data [ 0 ] as $ f => $ v ) { $ tmp [ ] = ":s_$f" ; } // make name space param for pdo insert statement $ sNameSpaceParam = implode ( ', ' , $ tmp ) ; // unset temp var unset ( $ tmp ) ; // get insert fields name $ sFields = implode ( ', ' , array_keys ( $ data [ 0 ] ) ) ; // handle safe mode. If it is set as false means user not using bind param in pdo if ( ! $ safeModeInsert ) { // set pdo insert statement in class property $ this -> sql = "INSERT INTO `$table` ($sFields) VALUES " ; foreach ( $ data as $ key => $ value ) { $ this -> sql .= '(' . "'" . implode ( "', '" , array_values ( $ value ) ) . "'" . '), ' ; } $ this -> sql = rtrim ( $ this -> sql , ', ' ) ; // return this object // return $this; // pdo prepare statement $ this -> STH = $ this -> prepare ( $ this -> sql ) ; // start try catch block try { // execute pdo statement if ( $ this -> STH -> execute ( ) ) { // store all last insert id in array $ this -> allLastId [ ] = $ this -> lastInsertId ( ) ; } else { self :: error ( $ this -> STH -> errorInfo ( ) ) ; } } catch ( \ PDOException $ e ) { // get pdo error and pass on error method self :: error ( $ e -> getMessage ( ) . ': ' . __LINE__ ) ; // PDO Rollback $ this -> back ( ) ; } // end try catch block // PDO Commit $ this -> end ( ) ; // close pdo $ this -> STH -> closeCursor ( ) ; // return this object return $ this ; } // end here safe mode // set pdo insert statement in class property $ this -> sql = "INSERT INTO `$table` ($sFields) VALUES ($sNameSpaceParam);" ; // pdo prepare statement $ this -> STH = $ this -> prepare ( $ this -> sql ) ; // set class property with array $ this -> data = $ data ; // set batch insert flag true $ this -> batch = true ; // parse batch array data foreach ( $ data as $ key => $ value ) { // bind pdo param $ this -> _bindPdoNameSpace ( $ value ) ; try { // execute pdo statement if ( $ this -> STH -> execute ( ) ) { // set class property with last insert id as array $ this -> allLastId [ ] = $ this -> lastInsertId ( ) ; } else { self :: error ( $ this -> STH -> errorInfo ( ) ) ; // on error PDO Rollback $ this -> back ( ) ; } } catch ( \ PDOException $ e ) { // get pdo error and pass on error method self :: error ( $ e -> getMessage ( ) . ': ' . __LINE__ ) ; // on error PDO Rollback $ this -> back ( ) ; } } // fine now PDO Commit $ this -> end ( ) ; // close pdo $ this -> STH -> closeCursor ( ) ; // return this object return $ this ; } else { self :: error ( 'Data not in valid format..' ) ; } } else { self :: error ( 'Table name not found..' ) ; } }
Execute PDO Insert as Batch Data.

@param string $table          mysql table name
@param array  $data           mysql insert array data
@param bool   $safeModeInsert set true if want to use pdo bind param

@return self last insert ID
Execute PDO Insert as Batch Data .