美文网首页
PHP实现.csv文件的上传与下载-Mysql

PHP实现.csv文件的上传与下载-Mysql

作者: 0d601f651140 | 来源:发表于2016-06-13 11:02 被阅读278次

    上传:

    <?php
    
    $mysql_hostname = "你的数据库IP地址";
    
    $mysql_user = "你的数据库登陆名";
    
    $mysql_password = "登陆密码";
    
    $mysql_database = "你的数据库中所使用的database";
    $prefix = "";
    
    //如果没有链接上,会报错“Opps some thing went wrong”
    $db = @mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
    mysql_select_db($mysql_database, $db) or die("Opps some thing wrong!!");
    mysql_query("set character set 'utf8'");
    mysql_query("set names 'utf8'");
    
    //fopen的地址是绝对路径,如果你使用的是linux,直接用PWD就可以把路径显示出来
    
    $file = fopen("/Applications/mamp/htdocs/address.csv", "r+") or exit("Unable to open file!");
    if (!feof($file)) {
    while ($data = fgetcsv($file)) {
    
     
    
    //data[0]位这个.csv文件的第一列
    
    $da = $data[0];
    var_dump($da);
    
    $sql = "INSERT INTO 你的表名 (字段名) 
    VALUES('" . $da . "')";
    
    mysql_query($sql);
    
    }
    }
    fclose($file);
    echo "数据导入成功";
    ?>
    

    下载

    <?php
           
    $mysql_hostname = "你的数据库IP地址";
    
    $mysql_user = "你的数据库登陆名";
    
    $mysql_password = "登陆密码";
    
    $mysql_database = "你的数据库中所使用的database";
    
    $sql = "SELECT DISTINCT a.id,company_name,region,contact,position,phone,industry,ticket,purpose,create_at FROM ims_register_app AS a LEFT JOIN ims_zyticketinfo AS b ON a.coupon_id=b.id where  create_at>'2014-12-31'";
            $result=mysql_query($sql);
            Header("Content-type: application/octet-stream; filename=\"专业观众信息.csv\"");
            Header("Content-Disposition: attachment; filename=\"专业观众信息.csv\"");
            //echo "id,单位名称,地区,联系人,职务,手机,所属行业,兑换码,参会目的,参会时间\n";
            echo iconv('utf-8','utf-8',"id,单位名称,地区,联系人,职务,手机,所属行业,兑换码,参会目的,参会时间\n");
        while($row= mysql_fetch_row($result))
            {
                //$row[9] = '"'.$row[9].'"';
                $row[8] = str_replace(","," ",$row[8]);
                for($i=0;$i < count($row);$i++)
                {
                    //echo $row[$i].",";
                    echo iconv('utf-8','utf-8',$row[$i]).",";
                }
                echo "\n";
            }
    ?>
    

    相关文章

      网友评论

          本文标题:PHP实现.csv文件的上传与下载-Mysql

          本文链接:https://www.haomeiwen.com/subject/ljdydttx.html