dongyou2305 2017-02-25 09:00
浏览 62
已采纳

使用PHP和MySQLi导入CSV替代方法

I am using the code below to import a CSV textfile into my table, but my new host not allowing LOAD DATA commands and forces me to find alternative ways of importing the file. Naturally, they are useless in giving me a clear answer.

What other ways except LOAD DATA LOCAL and LOAD DATA do I have to import a CSV file into my MySQL table via PHP/MySQLi?

  // Query - import CSV file into DB, ignore top 3 lines
    $sql= "LOAD DATA LOCAL INFILE '".$import_file."' INTO TABLE `$dbtable`
       FIELDS TERMINATED BY '".$fieldseparator."'
       LINES TERMINATED BY '".$lineseparator."'
       IGNORE 3 LINES
       SET TIMESTAMP = '".$local_time."'";

Thanks

EDIT:

After looking into the suggested fgetcsv function, I came up with the below code. Theoretically, it should work, but the code seems to load until it finally times out without showing an error. The database is populated with a single row entry only.

What am I doing wrong here?

                if ($import_file) {
                  // set row counter to 0
                  $line = 0;

                  while ( ($row = fgetcsv($import_file)) !== false ) {

                    // ignore the first 3 rows of the file based on iteration of the counter
                    if ($line <= 3) {
                      $line++;
                      continue;
                    } else {

                        // if row count is higher than 3, insert into database
                        $sql = "INSERT INTO `$dbtable`
                          (`OPERA_CONF`, `CRS`, `HOLIDEX`, `ARRIVAL`)
                          VALUES (
                            '" . $row[0] . "',
                            '" . $row[1] . "',
                            '" . $row[2] . "',
                            '" . $row[3] . "'
                          )";

                        // execute query
                        $mysqli->query($sql);
                        $line++;

                    }

                  }

                } else {
                    echo "
 CSV file could not be found.";
                }
  • 写回答

1条回答 默认 最新

  • dtslobe4694 2017-02-25 13:23
    关注

    You're going to have to use PHP to read the contents of the CSV file line by line and insert them into the table.

    Have a look at http://www.readmyviews.com/import-csv-data-mysql-using-php/

    Code pasted here for easy of use:

    $filepath = "C:\wamp\www\importdata\sample.csv"; 
    
    if (($getdata = fopen($filepath, "r")) !== FALSE) {
                   fgetcsv($getdata);   
                   while (($data = fgetcsv($getdata)) !== FALSE) {
                        $fieldCount = count($data);
                        for ($c=0; $c < $fieldCount; $c++) {
                          $columnData[$c] = $data[$c];
                        }
                 $option_name = mysqli_real_escape_string($connect ,$columnData[0]);
                 $option_value = mysqli_real_escape_string($connect ,$columnData[1]);
                 $import_data[]="('".$option_name."','".$option_value."')";
                // SQL Query to insert data into DataBase
    
                 }
                 $import_data = implode(",", $import_data);
                 $query = "INSERT INTO option_data_master(option_name,option_value) VALUES  $import_data ;";
                 $result = mysqli_query($connect ,$query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分