简体   繁体   English

将CSV文件导入数据库

[英]importing csv files into database

here is my code to import csv data to my database 这是我的代码将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>

I have a csv file where there is a column which contains emails 我有一个csv文件,其中有一个包含电子邮件的列

the import is done suvcessfull but the issue is that it just imports the value in another format like in other language or encripted or something un readable 导入已成功完成,但问题在于它只是以另一种格式(例如,其他语言或刻有文字或不可读的格式)导入值

Try this query to import csv from phpmyadmin or any other mysql client 尝试此查询从phpmyadmin或任何其他mysql客户端导入csv

Before firing query, please make sure that table matching the fields is already created into db. 触发查询之前,请确保与字段匹配的表已创建到db中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM