将csv文件导入数据库

时间:2015-12-08 16:28:11

标签: php csv import mysqlimport

这是我将csv数据导入数据库的代码


             
                   

if (isset($_POST["submit"])) {
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
**while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
    $name = $filesop[0];
     $email = $filesop[1];           
    $sql = mysql_query("INSERT INTO selleruser (emaili) VALUES ('$name')");
    $c = $c + 1;
    }**     
    if ($sql) {
        echo "You database has imported successfully. You have inserted ". $c ." recoreds";
    } else {
        echo "Sorry! There is some problem.";
    }
   }?>

</div>

我有一个csv文件,其中有一个包含电子邮件的列

导入完成suvcessfull但问题是它只是以其他语言或encripted或其他不可读的方式导入其他格式的值

1 个答案:

答案 0 :(得分:0)

尝试此查询从phpmyadmin或任何其他mysql客户端

导入csv

在触发查询之前,请确保已将匹配字段的表创建到db。

LOAD DATA LOCAL INFILE 'local machine path to csv file' INTO TABLE `selleruser`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(emaili);