dd

PDO 通过 ODBC 批量插入 MSSQL 数据(dataAll)

jerry thinkphp 2015年11月19日 收藏
ThinkPHP 3.2.* 对 ODBC 支持不是很好。所以,这里写了一个方法来实现 dataAll 操作——随笔即兴所写。。。
function dataAll($arr, $table){
    if($arr){
        foreach($arr as $m => $var){
            $varKeyList = array_keys($var);
            $varCount = count($varKeyList);
            
            for($i = 0; $i < $varCount; $i++){
                $insertList .= $varKeyList[$i];
                $insertValue .= "'".$var[$varKeyList[$i]]."'";
                
                if($i < $varCount - 1){
                    $insertList .= ",";
                    $insertValue .= ",";
                }
            }
            
            // 生成 SQL 语句
            $sql = "insert ".$table."(".$insertList.") values(".$insertValue.")";
            
            
            $Model = new \Think\Model();
            $Model->query(UTF8toGB($sql));

            $insertList = $insertValue = "";
        }
    }
}


// 编码转换(我的MSSQL 支持得是 GB2312)
function UTF8toGB($source){
    return iconv('UTF-8', 'gb2312//IGNORE', $source);
}
其中 $arr 是数组,$table 是表。
dd