zoukankan      html  css  js  c++  java
  • 将文件写进数据库的方法

    1。读取文件的内容

    2. 获得文件的长度

    3.数据库的连接(设置字符集   和选择要操作的数据库)

    4. 将文件的内容的字符串转化为数组(循环,执行sql语句)

    5.关闭数据库

    <?php
        header("Content-Type:text/html;charset=utf-8");
        //1.读取文件到数组
        $file = file('tianqi.txt');
        //print_r($file);
        //2.计算数组长度
        $count = count($file);
        //3.导入数据到数据库
        mysql_connect('localhost','root','');
        mysql_query('use tianqi');
        mysql_query('set names utf8');
    
        //
        for($i=0;$i<$count;$i++){
            $data = explode('=',$file[$i]);
            $code = $data[0];
            $name = $data[1];
    
            //组装sql语句
            $sql = "insert into city values(null,'$code','$name')";
            mysql_query($sql);
        }
    
        mysql_close();
  • 相关阅读:
    学习笔记::有上下界的网络流
    zoj2314
    bzoj3261
    bzoj 1898
    bzoj4009
    bzoj4033
    bzoj3389
    bzoj2427
    uva 11825
    交换A与B值的四种方法
  • 原文地址:https://www.cnblogs.com/zhengweizhao/p/6918170.html
Copyright © 2011-2022 走看看