php数据库批量插入实例

发布时间:2020-05-09编辑:脚本学堂
本文介绍了php实现数据库批量插入的一个例子,php批量插入数据实现代码,有需要的朋友参考下。

例子,php批量插入数据的类。
 

复制代码 代码示例:
<?php 
class dbModel {     
    protected $fields =   array(); 
     
    /**
     * @desc 批量添加
     * 
     * @param Array $dataList
     * @param String $tableName
     * @return boolean
     */ 
    public function addAll($dataList,$tableName) { 
        if (! is_array ( $dataList ) || empty($tableName))  return false; 
        $this->fields = $this->getFields($tableName); 
        foreach ( $dataList as $key => $val ) { 
            $dataList[$key] = $this->_facade($val); 
        } 
        return $this->insertAll($dataList, $tableName); 
    } 
     
 
    /**
     * @desc 批量插入数据
     * 
     * @param Array $datas
     * @param String $tableName
     * @return boolean|Ambigous <boolean, resource>
     */ 
    protected function insertAll($datas,$tableName) { 
        if(!is_array($datas[0])) return false; 
        $fields = array_keys($datas[0]); 
        array_walk($fields, array($this, 'parseKey')); 
        $values  =  array(); 
        foreach ($datas as $data){ 
            $value   =  array(); 
            foreach ($data as $key=>$val){ 
                if(is_scalar($val)) { // 过滤非标量数据 
                    $value[]   =  $val; 
                } 
            } 
            $values[]    = '('.implode(',', $value).')'; 
        } 
        $sql   =  'INSERT INTO '.$tableName.' ('.implode(',', $fields).') VALUES '.implode(',',$values); 
        return $this->mysql->query($sql); 
    } 
     
     
    /**
     * 
     * @desc 得到数据表结构
     * @param String $tableName
     * @return Array $info
     */ 
    protected function getFields($tableName) { 
        $result = $this->mysql->findAll ( 'SHOW COLUMNS FROM ' . $tableName ); 
        $info = array (); 
        foreach ( $result as $key => $val ) { 
            $info [$val ['Field']] = array ( 
                    'name' => $val ['Field'], 
                    'type' => $val ['Type'], 
                    'notnull' => ( bool ) ($val ['Null'] === ''),   
                    'default' => $val ['Default'], 
                    'primary' => (strtolower ( $val ['Key'] ) == 'pri'), 
                    'autoinc' => (strtolower ( $val ['Extra'] ) == 'auto_increment')  
            ); 
        } 
        return $info; 
    } 
     
    /**
     * 
     * @desc 过滤数据库中没有的字段数据
     * @param Array $data
     * @return Array $data
     */ 
    protected function _facade(&$data){ 
        if(!empty($this->fields)){ 
            $fields = array_keys($this->fields); 
            foreach($data as $key=>$val){ 
                if(!in_array($key,$fields,true)){ 
                    unset($data[$key]); 
                }else if(is_scalar($val)){ 
                    // 字段类型检查 
                    $this->_parseType($data,$key); 
                } 
            } 
        } 
        return $data; 
    } 
     
    /**
     * @desc 转义类型
     * 
     * @param Array $data
     * @param String $key
     */ 
    protected function _parseType(&$data,$key){ 
        $fieldType = strtolower($this->fields[$key]['type']); 
        if(false !== strpos($fieldType,'char')){ 
            $data[$key]   =  '''.mysql_real_escape_string($data[$key]).'''; 
        }elseif(false === strpos($fieldType,'bigint') || false !== strpos($fieldType,'int')) { 
            $data[$key]   =  intval($data[$key]); 
        }elseif(false !== strpos($fieldType,'float') || false !== strpos($fieldType,'double')){ 
            $data[$key]   =  floatval($data[$key]); 
        }elseif(false !== strpos($fieldType,'bool')){ 
            $data[$key]   =  (bool)$data[$key]; 
        } 
    } 
     
    /**
     * @desc 转义非法字段名称
     * 
     * @param String $key
     * @return string $key
     */ 
    protected function parseKey(&$key) { 
        $key   =  trim($key); 
        if(!preg_match('/[,'"*()`.s]/',$key)) { 
            $key = '`'.$key.'`'; 
        } 
        return $key; 
    }     
}