欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP中txt导入mysql代码

程序员文章站 2022-05-15 09:50:45
...
注意在导入时保持数据库的编码与txt文档编码尽量一样,要不可能出现乱码问题哦。

注意在导入时保持数据库教程的编码与txt文档编码尽量一样,要不可能出现乱码问题哦。

";
        if($_FILES["insert_first"]["error"]){
          echo "上传错误:".$_FILES["insert_first"]["error"]."
"; }else{ echo "文件名称:".$_FILES["insert_first"]["name"]."
"; echo "文件类型:".$_FILES["insert_first"]["type"]."
"; echo "文件大小:".($_FILES["insert_first"]["size"]/1024)."Kb
"; echo "临时名称:".$_FILES["insert_first"]["tmp_name"]."
"; } }else{ echo "上传文件格式不正确"; exit; } //连接数据库 $con = new mysql教程i('localhost','s503440db0','td9928ps教程','s503440db0'); if(mysqli_connect_errno()){ echo "链接数据库失败"; exit; } //打开文件 $file = fopen($_FILES["insert_first"]["tmp_name"],"r") or exit("打开文件失败!"); var_dump($file); echo "
"; while(!feof($file)){ //逐行分拆 $row = explode(",",fgets($file)); $row0 = $row[0]; $row1 = (string)$row[1]; echo "$row0"."---"."$row1"."
"; //判断记录是否存在 $query1 = "select firstid from firstcategory where firstid=$row0"; $query2 = "select firstname from firstcategory where firstname='$row1'"; $result1 = $con->query($query1); $result2 = $con->query($query2); //判断行数 $result1 = mysqli_num_rows($result1); $result2 = mysqli_num_rows($result2); if($result1){ echo "行号{$row0}已存在
"; }elseif($result2){ echo "分类{$row1}已存在
"; }else{ $query = "insert into firstcategory (firstid,firstname) values ($row0,'音乐')"; $result = $con->query($query); } } echo "导入结束,请检查数据是否正确 http://www.phprm.com "; fclose($file); $con->close();

mysql数据库

"create table firstcategory (
          firstid int unsigned not null primary key,
                  firstname char(50) not null                
                )";

教程网址:

欢迎收藏∩_∩但请保留本文链接。