Home > php教程 > php手册 > PHP中txt导入mysql代码

PHP中txt导入mysql代码

WBOY
Release: 2016-05-25 16:56:42
Original
1668 people have browsed it
注意在导入时保持数据库的编码与txt文档编码尽量一样,要不可能出现乱码问题哦。

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

<?php教程
  //验证文件类型和错误
if($_FILES["insert_first"]["type"] == "text/plain"){
    echo "第一步:文件类型上传正确"."<br/>";
        if($_FILES["insert_first"]["error"]){
          echo "上传错误:".$_FILES["insert_first"]["error"]."<br/>";
        }else{
          echo "文件名称:".$_FILES["insert_first"]["name"]."<br/>";
          echo "文件类型:".$_FILES["insert_first"]["type"]."<br/>";
          echo "文件大小:".($_FILES["insert_first"]["size"]/1024)."Kb<br/>";
          echo "临时名称:".$_FILES["insert_first"]["tmp_name"]."<br/>";
        }        
  }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 "<br/>";
while(!feof($file)){  
  //逐行分拆
  $row = explode(",",fgets($file));
  $row0 = $row[0];
  $row1 = (string)$row[1];
  echo "$row0"."---"."$row1"."<br/>";
  //判断记录是否存在
        $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}已存在<br/>";
        }elseif($result2){
          echo "分类{$row1}已存在<br/>";
        }else{
          $query = "insert into firstcategory (firstid,firstname) values ($row0,'音乐')";
          $result = $con->query($query);
        } 
}
echo "导入结束,请检查数据是否正确 http://www.phprm.com ";

fclose($file); 
$con->close();
Copy after login

mysql数据库

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

教程网址:

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template