用php加入csv中的部分行

时间:2014-05-28 15:25:57

标签: php mysql sql csv

我有一个像这样的csv文件:

"Name","surname","note"
"Jhon","Jhon","fdskngkfngkfnkglnlksanjlbgsjangkjlvnkajsdf"
"Jhon2","Jhon2","fdssfsfsfkngkfngkfnkglnlksanjlbgsjangkjlvnkajsdf"
"Jhon3","Jhon3","fdssfsfsfkngkfngkfnk
glnlksanjlbgsjangkjlvnkajsdf"
"Jhon4","Jhon4","fdssfsfsf
kngkfngkfnkglnlks
anjlbgsjangkjlvnkajsdf"

我需要加入返回的行,因为我需要在mySQL中添加这个文件。

我使用此代码

$csv_file = "output.csv";
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;

while (!feof($csvfile))
{
   $csv_data[] = fgets($csvfile, 1024);
   $csv_array = explode('","', $csv_data[$i]);
   $insert_csv = array();

    $insert_csv['Gestore'] = ltrim($csv_array[0], '"'); 

   echo $insert_csv['Gestore'] . "</br>";
   $query = "INSERT INTO prova(Gestore) VALUES('".$insert_csv['Gestore']."')";
   $n=mysql_query($query, $connect );
   $i++;
}

它有效,但我需要解决先例问题......

1 个答案:

答案 0 :(得分:0)

您只需使用PHP函数fgetcsv

作为PHP的替代品,您可以使用

LOAD DATA INFILE

如果您拥有FILE权限,请参阅MySQL manual, LOAD DATA INFILE