【问题标题】:csv file is inserting a blank row in the last of other rows into mysql tablecsv 文件正在将其他行的最后一行中的空白行插入 mysql 表
【发布时间】:2015-02-27 13:19:05
【问题描述】:

当我将 csv 文件中的记录插入 mysql 表时,它还会插入一个空白行,其他行位于另一条记录的最后。

这是我的代码:

<?php
     $connect = mysql_connect('localhost','root','root');
    if (!$connect) {
    die('Could not connect to MySQL: ' . mysql_error());
    }
   $cid =mysql_select_db('bizin490_devcredit',$connect);
      define('CSV_PATH', '/home/ubc/Documents/');
     $csv_file = CSV_PATH . "test.csv";
            $csvfile  = fopen($csv_file, 'r');
            $theData  = fgetcsv($csvfile);
            $i = 0;
            while (!feof($csvfile)) {
                $csv_data[] = fgets($csvfile, 1024);
                $csv_array  = explode(",", $csv_data[$i]);
                $insert_csv = array();
                $insert_csv['name']  = $csv_array[0];
                $insert_csv['email'] = $csv_array[1];
                $query = mysql_query("select name from test where name='" . $insert_csv['name'] . "'");
                $count = mysql_num_rows($query);
                if ($count == 0) {
                    $query = "INSERT INTO test(name,email)VALUES('" . $insert_csv['name'] . "','" . $insert_csv['email'] ."')";
                        $n = mysql_query($query, $connect);
                } else {
                    $sql = "update test set email='".$insert_csv['email']."'";
                            $qu  = mysql_query($sql);       
                }
     $i++;
                }

     fclose($csvfile);

            echo "File data successfully imported to database!!";
            mysql_close($connect);
            ?>

【问题讨论】:

  • 不要使用 fgets()。如果您不能使用 LOAD DATA INFILE,那么至少使用 fgetcsv() 并为自己省去爆炸的任意性和担心引用的字符串
  • 并检查$csv_array[0] 是否为空,如果不为空,则仅执行选择/插入/更新
  • 使用SPLFileObject 更好,因为你可以让它自动忽略空行

标签: php mysql csv


【解决方案1】:

效率不是很高,但添加一个条件

if ($insert_csv['name'] !== '' && $insert_csv['email']!=='') //since != '' will return true if you pass is numeric 0 and a few other cases, hence using !==
{
      $query = mysql_query("select name from test where name='" . $insert_csv['name'] . "'");
      $count = mysql_num_rows($query);
     if ($count == 0) {
                $query = "INSERT INTO test(name,email)VALUES('" . $insert_csv['name'] . "','" .  $insert_csv['email'] ."')";
                $n = mysql_query($query, $connect);
     } else {
                $sql = "update test set email='".$insert_csv['email']."'";
                $qu  = mysql_query($sql);       
     }

}

【讨论】:

    【解决方案2】:

    使用 SplFileObject,你可以让它忽略空行,并自动将文件作为 CSV 文件处理

    $file = new SplFileObject($csv_file);
    $file->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY);
    foreach ($file as $csv_array) {
        ...
    }
    

    【讨论】:

      【解决方案3】:

      这段代码可能存在缺陷:

              $csvfile  = fopen($csv_file, 'r');           // opens the file
              $theData  = fgetcsv($csvfile);               // reads first line
              $i = 0;
              while (!feof($csvfile)) {                    // checks for eof
                  $csv_data[] = fgets($csvfile, 1024);     // reads next line
      
      1. 当没有标题时,第一行被跳过。
      2. feof 在读完最后一行之后是 falsefgets不检查,最后一个\n后面是否有数据,所以feof为假。 fgets 然后读取“空”行,直到错误添加的 eof

      变化:

        // checks for eof and errors
        while ( ($csv_data[] = fgets($csvfile, 1024)) !== false) {  
             ...
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-26
        • 2019-04-04
        • 2019-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多