简体   繁体   English

使用php将CSV文件加载到mysql数据库中

[英]Loading CSV file into mysql database using php

I ma struggling to load information from a csv file using PHP and want to save the contents of csv (mobile phone numbers) into mysql database table.The file contents look like this: 我正在努力使用PHP从csv文件中加载信息,并希望将csv(手机号码)的内容保存到mysql数据库表中。文件内容如下所示:

CSV file contents ( individual record per single line with no commas) CSV文件内容(每行单条记录,不带逗号)

44762126064    
447508751    
4474669756    
44771466603    
444584871    
445574805    
447455471039    
44777451345
447460345819    
44793342963    
44734838672    
44752845528    
4474537291    
44779645078

I am try to upload csv file using form and submit.The code read csv file and tries to write the content into mysql table.The problem is that code is returning all the csv records in one array element like this: 我尝试使用表单上传csv文件并提交。代码读取csv文件,然后尝试将内容写入mysql表中。问题是代码将所有csv记录返回到一个数组元素中,如下所示:

Array( 
    [0] => 44762126064 447508751 4474669756 44771466603 444584871 445574805 447455471039 44777451345 447460345819 44793342963 44734838672 44752845528 4474537291 44779645078 
);

and inserting the whole array as one record rather one mobile number per row in mysql table. 并将整个数组作为一条记录插入,而不是在mysql表中每行插入一个手机号码。

The code : 编码 :

if (isset($_POST['submit'])) {
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
        echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
    echo "<h2>Displaying contents:</h2>";
    #    readfile($_FILES['filename']['tmp_name']);
}

//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

           $import="INSERT into mobilecsv(phoneMobile,status) values('$data[0]',0)";

    mysql_query($import) or die(mysql_error());
}

fclose($handle);

print "Import done";

}

I have tried explode() , array_slice() etc but none of them helped me to split this array contents to save them as individual records/phone numbers in mysql database. 我尝试了explode()array_slice()等,但是它们都没有帮助我拆分数组内容以将它们另存为mysql数据库中的单独记录/电话号码。

How can i split individual contents of first array element(as in my case) to save them as separate individual records as appearing in my CSV? 如何拆分第一个数组元素的单个内容(如我的情况),以将它们另存为单独的单个记录,如出现在CSV中? I am a new programmer and would appreciate your help in this regard.Thanks 我是一名新程序员,非常感谢您在这方面的帮助。

The problem is fgetcsv isn't detecting a comma which is at the heart of a COMMA separated value (CSV) file. 问题是fgetcsv没有检测到逗号,而逗号是COMMA分隔值(CSV)文件的核心。 Add this line at the top of your file after the php opening tag: 在php开头标签之后,在文件顶部添加以下行:

ini_set("auto_detect_line_endings", true);

change your import code to: 将您的导入代码更改为:

//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while(($data = fgetcsv($handle)) !== FALSE){
$phoneMobile = $line[0];

$import="INSERT into mobilecsv(phoneMobile,status) values('$phoneMobile',0)";
mysql_query($import) or die(mysql_error());
}

fclose($handle);

print "Import done";

}

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

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