将文本里的数据使写入MYSQL.
这时有一个问题,
看表吧
这是数据库里面的
库名:phone
表名:bc_haoduan
字段:    mobile              city
           1313456              北京
           1353456              上海
文本 phone.txt
要导入数据库(库名phone)的表是 telphone
里面有如下字段:
mobile            dtime             uid                   city
现在要求:
将文本导入到telphone的时候,与bc_haoduan中的mobile比较
当文本内的mobile内容在bc_haoduan中的mobile范围之内,
city便自动插入bc_haoduan里的city

解决方案 »

  1.   

    file将txt中的每行读成他的一个元素,多个字段在一行你可以explode分解它们
      

  2.   


    //读取文件中的单条mobile数据
    function read($file){
    .......
    .......
    return $xx.'|'.$xx;//返回$mobile和$city
    }
    $ct = explode('|',read(phone.txt));
    $mobile=$ct[0];
    $city=$ct[1];
    $sql = "select mobile from phone.bc_haoduan where mobile={$mobile}";
    .........//数据连接,查询结果$rst
    if(!$rst){
    $sql = "update phone.bc_haoduan  set city='{$city}' where mobile ={$mobile}";
    .......//执行$sql语句
    }
    当然你也可以把上面这段代码写成数据库里的触发器!
      

  3.   

    文件读取可参考我昨天写的一个文件规则过滤类<?php
    /**
     * @=STATIC
     * 
     * 针对文件内容规则的检测
     * @author yichen
     * @history 2010-11-02
     *  
     */class CK_coding{
        
        
        /***用于数据量大的文件***/
        public static function readLine($path, $line_num, $delimiter="\n")
        {
            /***设置读取一行***/
            $i = 1;
        
            /***读取模式打开文件***/
            $fp = fopen( $path, 'r' );
        
            /*** 循环读取 ***/
            while ( !feof ( $fp) )
            {
                /*** 读取一行内容到内存中 ***/
                $buffer = stream_get_line( $fp, 1024, $delimiter );
                /*** 假如到达查找的那一行 ***/
                if( $i == $line_num )
                {
                    /*** 返回在内存中那一行的内容 ***/
                    return str_replace(array("\n","\r"),'',$buffer);
                }
                $i++;
                /*** 清除内存 ***/
                $buffer = '';
            }
            return false;
        }
        
        
        /***用于数据量小的文件***/
        public static function getline($ct,$num=1){
            $x = split("\n",$ct);
            $num--;
            return str_replace(array("\n","\r"),'',$x[$num]);
        }    /***判断是否是utf-8编码***/
        public static function utf8char($ct)
        {
            return ($ct === mb_convert_encoding(mb_convert_encoding($ct, "UTF-32", "UTF-8"), "UTF-8", "UTF-32"))? true : false;
        }
        
        
        /*****文件中出现的字符只能在范围:\x{4e00}-\x{9fa5}汉字  \x{3000}-\x{303f}标点  \x{0000}-\x{0070}基本acsii *******/
        public static function ctchar($ct){
            if(!preg_match("/^([\x{4e00}-\x{9fa5}]|[\x{3000}-\x{303f}]|[\x{0000}-\x{0070}]|[a-zA-Z]|[{}])+$/u",$ct)){return false;}  
            return true;
        }
        
        public static function getlinenum($ct){
            $x = split("\n",$ct);
            return count($x);
        }
        
        
        /***检测程序行开头***/
        public static function ckstline($ct,$num=1){
            $line = str_replace("\t",'',$ct);
            if(!preg_match("/^\S+/",$line)){
                exit("第{$num}行开头存在空格");
            }
        }
        
        
        /***文件每行只能有一个引号外的';'号***/
        public static function ($line){
            $num1 = substr_count($line,';');
            preg_match("/.*\".*\;+\"/",$line,$match1);
            preg_match("/.*\'.*\;+\'/",$line,$match2);
            $num2 = count($match1)+count($match2);
            //echo($num1.'-'.count($match1).'-'.count($match2).'<br>');
            if(($num1-$num2)>1){return false;}
            return true;
        }
        
        /***检测文件行数限制规则***/
        public static function numrule($tp,$num,$ct){
            switch ($tp)
            {
            case 'LIST':
                if($num>200){exit('LIST类型代码行数不能超过200');} 
                break;
            case 'SCRIPT':
                if($num>1000){exit('SCRIPT类型代码行数不能超过1000');}
                break;
            case 'STATIC':
                if($num>1000){exit('STATIC类型代码行数不能超过1000');}
                break;
            case 'HACK':
                
                break;                               
            }
            
            $notenum['LIST'] = 4;
            $notenum['SCRIPT'] = 5;
            preg_match_all("/.*SECT\-+\d{1}\-START+.*/",$ct,$match);
            $countnote = count($match[0]);
            /**SECT-3-START**/
            switch($tp)
            {
            case "LIST":
                if($countnote!=$notenum['LIST']){exit("{$tp}类型必须有{$notenum['LIST']}段注释");}         
                break;
            case "SCRIPT":
                if($countnote!=$notenum['SCRIPT']){exit("{$tp}类型必须有{$notenum['SCRIPT']}段注释");}        
                break;
            case "STATIC":
                        
                break;
            case "HACK":
                            
                break;
            }        
            
            
        }
        
        
        /***检测文件规则***/
        public static function ck($path){
            $ct = file_get_contents($path);
            
            if(!self::utf8char($ct)){
                exit('文件编码方式必须为UTF8');
            }
            if(!self::ctchar($ct)){
                exit('文件中出现的字符超过范围');
            }
            
            
            $line1 = self::getline($ct);
            if($line1!="<?php"){
                exit("文件第一行必须为<?php");
            }
            
            $line2 = self::getline($ct,2);
            if($line2!='/**'){
                exit('文件第二行必须为/**');
            }
            
            
            $line3 = self::getline($ct,3);
            if($line3!=' * @=LIST' && $line3!=' * @=SCRIPT' && $line3!=' * @=STATIC' && $line3!=' * @=HACK'){exit('文件第三行不符合格式');}
            //文件类型
            $filetp = substr($line3,5);
            
            
            $num = self::getlinenum($ct);
            $coderow = 0;
            for($i=1;$i<$num+1;$i++){
                $line = self::getline($ct,$i);
                $linect = str_replace(array("\t",' '),'',$line);
                //如果该行为注释或者空则跳过检测
                if(preg_match("/^([\/\*])+/",$linect) || $linect==''){continue;}
                self::ckstline($line,$i);
                if(!self::($line)){exit("文件第{$i}行错误,只能有一个引号外的';'号");}
                $coderow++;//代码行数+1
                
                if(preg_match("/^class\s+\w+\{+$/",$line,$new)){print_r ($new);$note=2;}
                
            }
            
            
            self::numrule($filetp,$coderow,$ct);
            
            
        }
    }?>