PHP:如何将 CSV 文件的内容逐行导入 MySQL 数据库?

发布于 2024-10-19 08:03:37 字数 1238 浏览 1 评论 0原文

如何将 CSV 文件的内容逐行存入 MySQL 数据库?我尝试了几种方法,但使用 fgetcsv 永远无法返回多于一行。我尝试过的一种方法似乎非常接近工作:

    $fileName = $_FILES['SpecialFile']['name'];
    $tmpName = $_FILES['SpecialFile']['tmp_name'];
    $fileSize = $_FILES['SpecialFile']['size'];

    if(!$fileSize) 
    {
        echo "File is empty.\n";
        exit;
    }

    $fileType = $_FILES['SpecialFile']['type'];
    $file = fopen($tmpName, 'r');

    if(!$file) 
    {
        echo "Error opening data file.\n";
        exit;
    }

    while(!feof($file))
    {
        $data = str_replace('"','/"',fgetcsv($file, filesize($tmpName), ","));

        $linemysql = implode("','",$data);

        $query = "INSERT INTO $databasetable VALUES ('$linemysql')";

        return mysql_query($query);

    }

    fclose($file);

只输入一行,但如果我 print_r $data 它会返回所有行。如何让它插入所有行?

另一种方法:

    $data = str_getcsv($csvcontent,"\r\n","'","");

    foreach($data as &$Row) 
    {
        $linearray = str_getcsv($Row,',',''); //parse the items in rows 

        $linemysql = implode("','",$linearray);

        echo $query = "INSERT INTO $databasetable VALUES ('$linemysql')";
    }

这几乎也有效,但是 csv 中的文本也包含新行,所以我不知道如何分割实际行而不是文本中的新行。

How can I get the contents of a CSV file into a MySQL database row by row? Ive tried a few methods but can never get more than one row returned, using fgetcsv. One method I've tried that seemed to come so close to working:

    $fileName = $_FILES['SpecialFile']['name'];
    $tmpName = $_FILES['SpecialFile']['tmp_name'];
    $fileSize = $_FILES['SpecialFile']['size'];

    if(!$fileSize) 
    {
        echo "File is empty.\n";
        exit;
    }

    $fileType = $_FILES['SpecialFile']['type'];
    $file = fopen($tmpName, 'r');

    if(!$file) 
    {
        echo "Error opening data file.\n";
        exit;
    }

    while(!feof($file))
    {
        $data = str_replace('"','/"',fgetcsv($file, filesize($tmpName), ","));

        $linemysql = implode("','",$data);

        $query = "INSERT INTO $databasetable VALUES ('$linemysql')";

        return mysql_query($query);

    }

    fclose($file);

only enters one row, but if I print_r $data it returns all the rows. How do I get it to insert all th rows?

Another method:

    $data = str_getcsv($csvcontent,"\r\n","'","");

    foreach($data as &$Row) 
    {
        $linearray = str_getcsv($Row,',',''); //parse the items in rows 

        $linemysql = implode("','",$linearray);

        echo $query = "INSERT INTO $databasetable VALUES ('$linemysql')";
    }

This almost works too, but there is text within the csv that also contains new lines, so I dont know howto split the actual rows and not the new lines in the text as well.??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2024-10-26 08:03:37

该函数从 csv 文件返回一个数组

function CSVImport($file) {
    $handle = fopen($file, 'r');
    if (!$handle)
        die('Cannot open  file.');
    $rows = array();
    //Read the file as csv
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {

        $rows[] =  $data

    }

    fclose($handle);

    return $rows;
}

// 这将返回一个数组
// 创建一些逻辑来读取数组并保存它

$csvArray = CSVImport($tmpName);
        if (count($csvArray)) {
             foreach ($csvArray as $key => $value) {

                  // $value is a row of adata 

              }
          }

this function return an array from csv file

function CSVImport($file) {
    $handle = fopen($file, 'r');
    if (!$handle)
        die('Cannot open  file.');
    $rows = array();
    //Read the file as csv
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {

        $rows[] =  $data

    }

    fclose($handle);

    return $rows;
}

// this will return an array
// make some logic to read the array and save it

$csvArray = CSVImport($tmpName);
        if (count($csvArray)) {
             foreach ($csvArray as $key => $value) {

                  // $value is a row of adata 

              }
          }
陪我终i 2024-10-26 08:03:37

我想这就是您正在寻找的。

function getCSVcontent($filePath) {
    $csv_content = fopen($filePath, 'r');

    while (!feof($csv_content)) {
        $rows[] = fgetcsv($csv_content,1000,";");
    }
    fclose($csv_content);
    return $rows;
}

确保新行分隔符是“;”或者将正确的值提供给 fgetcsv()。问候。

I think this is what you are looking for.

function getCSVcontent($filePath) {
    $csv_content = fopen($filePath, 'r');

    while (!feof($csv_content)) {
        $rows[] = fgetcsv($csv_content,1000,";");
    }
    fclose($csv_content);
    return $rows;
}

Make sure that you new line separator is ";" or give the correct one to fgetcsv(). Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文