public static function makeInserts($tbl, $rows, array $fields = [], $implode = ";\n", $query_max_len = 524288): array
    {
        if ($query_max_len > 1048575) {
            throw new InvalidArgumentException('Each query should be shorter than 1MB.');
        }

        if ($fields) {
            foreach ($fields as & $v) {
                $v = '`' . $v . '`';
            }
            unset($v);
            $fields = ' (' . implode(', ', $fields) . ')';
        } else {
            $fields = '';
        }

        $tmp = $res = [];
        $tmp_len = 0;

        foreach ($rows as $line) {
            foreach ($line as & $v) {
                $v = "'" . self::sql_prepare($v) . "'";
            }
            unset($v);

            $q = '(' . implode(', ', $line) . ')';
            $tmp[] = $q;
            $tmp_len += \strlen($q);

            // Make all inserts in multiple queries
            if ($tmp_len >= $query_max_len) {
                $res[] = 'INSERT INTO `' . $tbl . '`' . $fields . ' VALUES ' . implode(', ', $tmp);
                $tmp = [];
                $tmp_len = 0;
            }
        }

        if ($tmp) {
            $res[] = 'INSERT INTO `' . $tbl . '`' . $fields . ' VALUES ' . implode(', ', $tmp);
        }

        return $implode ? implode($implode, $res) : $res;
    }
public static function makeInserts($tbl, $rows, array $fields = [], $implode = ";\n", $query_max_len = 524288): array
    {
        if ($query_max_len > 1048575) {
            throw new InvalidArgumentException('Each query should be shorter than 1MB.');
        }

        if ($fields) {
            foreach ($fields as & $v) {
                $v = '`' . $v . '`';
            }
            unset($v);
            $fields = ' (' . implode(', ', $fields) . ')';
        } else {
            $fields = '';
        }

        $tmp = $res = [];
        $tmp_len = 0;

        foreach ($rows as $line) {
            foreach ($line as & $v) {
                $v = "'" . self::sql_prepare($v) . "'";
            }
            unset($v);

            $q = '(' . implode(', ', $line) . ')';
            $tmp[] = $q;
            $tmp_len += \strlen($q);

            // Make all inserts in multiple queries
            if ($tmp_len >= $query_max_len) {
                $res[] = 'INSERT INTO `' . $tbl . '`' . $fields . ' VALUES ' . implode(', ', $tmp);
                $tmp = [];
                $tmp_len = 0;
            }
        }

        if ($tmp) {
            $res[] = 'INSERT INTO `' . $tbl . '`' . $fields . ' VALUES ' . implode(', ', $tmp);
        }

        return $implode ? implode($implode, $res) : $res;
    }
public static function makeInserts ( $ tbl , $ rows , array $ fields = [ ] , $ implode = ";\n" , $ query_max_len = 524288 ) : array { if ( $ query_max_len > 1048575 ) { throw new InvalidArgumentException ( 'Each query should be shorter than 1MB.' ) ; } if ( $ fields ) { foreach ( $ fields as & $ v ) { $ v = '`' . $ v . '`' ; } unset ( $ v ) ; $ fields = ' (' . implode ( ', ' , $ fields ) . ')' ; } else { $ fields = '' ; } $ tmp = $ res = [ ] ; $ tmp_len = 0 ; foreach ( $ rows as $ line ) { foreach ( $ line as & $ v ) { $ v = "'" . self :: sql_prepare ( $ v ) . "'" ; } unset ( $ v ) ; $ q = '(' . implode ( ', ' , $ line ) . ')' ; $ tmp [ ] = $ q ; $ tmp_len += \ strlen ( $ q ) ; // Make all inserts in multiple queries if ( $ tmp_len >= $ query_max_len ) { $ res [ ] = 'INSERT INTO `' . $ tbl . '`' . $ fields . ' VALUES ' . implode ( ', ' , $ tmp ) ; $ tmp = [ ] ; $ tmp_len = 0 ; } } if ( $ tmp ) { $ res [ ] = 'INSERT INTO `' . $ tbl . '`' . $ fields . ' VALUES ' . implode ( ', ' , $ tmp ) ; } return $ implode ? implode ( $ implode , $ res ) : $ res ; }
Insert a lot of values at once using multiple queries to prevent memory overflow

@param string $tbl - table name
@param array $rows - rows to be inserted
@param array $fields - fields keys and values, name and content
@param string $implode
@param int $query_max_len

@return array
@throws InvalidArgumentException
Insert a lot of values at once using multiple queries to prevent memory overflow