【问题标题】:Inserting CSV into MYSQL using PHP使用 PHP 将 CSV 插入 MYSQL
【发布时间】:2016-07-28 07:05:14
【问题描述】:

我知道有很多与此相关的问题,但是我似乎无法为我的错误找到解决方案。

我的 CSV:

我的 SQL 表:

我想在 MYSQL 中插入 CSV。

我知道我不需要为此编写代码,但我希望数据在 MYSQL 中,因为用于检索代码的 python 代码每 1 小时刷新一次。

这是我的代码:

第一次尝试

<?php

//database connection details
$connect = mysql_connect('127.0.0.1','admin','password');

if (!$connect) {
 die('Could not connect to MySQL: ' . mysql_error());
}

//your database name
$cid =mysql_select_db('weather',$connect);

// path where your CSV file is located
define('CSV_PATH','C:/scripts/');

// Name of your CSV file
$csv_file = CSV_PATH . "nea.csv"; 


if (($handle = fopen($csv_file, "r")) !== FALSE) {
   fgetcsv($handle);   
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    for ($c=0; $c < $num; $c++) {
      $col[$c] = $data[$c];
    }

// column name in mysql = column in csv?
 $validTime = $col[0];
 $Date = $col[1];
 $Time = $col[2];
 $Lat = $col[3];
 $Lon = $col[4];
 $AreaName = $col[5];

// SQL Query to insert data into DataBase
$query = "INSERT INTO neaweather(validTime, Date, Time, Lat, Lon, AreaName)   
VALUES('".$col[0]."','".$col[1]."','".$col[2]."','".$col[3]."','".$col[4]."','".$col[5]."')";
$s = mysql_query($query, $connect );
 }
  fclose($handle);
 }

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

第二次尝试:

<?php
//set the connection variables
$hostname = "127.0.0.1";
$username = "admin";
$password = "password";
$database = "weather";
$filename = "C:/scripts/nea.csv";

//connect to mysql database
$connection = mysqli_connect($hostname, $username, $password, $database) or    
    die("Error " . mysqli_error($myConnection));

// open the csv file
$fp = fopen($filename,"r");

//parse the csv file row by row
 while(($row = fgetcsv($fp,"500",",")) != FALSE)
 {
 $sql = "INSERT INTO neaweather (ValidTime, Date, Time, Lat, Lon, AreaName) VALUES ( '".mysqli_escape_string($data[0])."','".mysqli_escape_string($data[1])."','".mysqli_escape_string($data[2])."','".mysqli_escape_string($data[3])."','".mysqli_escape_string($data[4])."','".mysqli_escape_string($data[5])."')";
    mysqli_query($myConnection,$sql);
    if($query){
        echo "row inserted\n";
    }
    else{
        echo die(mysqli_error());
    }
}
fclose($fp);

//close the db connection
mysqli_close($myConnection);
?>

编辑:我运行了两个代码,完全没有错误。但是数据还是没有进入MYSQL

【问题讨论】:

  • Access denied for user ''@'localhost' &lt;using password : NO &gt; 表示您没有向 mysql 发送任何凭据,因此未建立连接。这可能是因为您将连接设为 $connection,但稍后在代码中尝试使用 $myConnection
  • 我没有向 mysql 发送凭据是什么意思? :x
  • 您需要为 MySQL 提供正确的登录名/密码
  • 即使连接成功,插入也会失败,因为$data虽然没有定义$row
  • 我的意思是您将 mysqli_* 函数与 mysql_* 函数混合在一起,导致在没有凭据的情况下打开第二个连接(查看您的插入 SQL)

标签: php mysql csv


【解决方案1】:

如果你使用这个库,我想建议你,它支持 csv,xls,xlsx 文件的导入和导出。

我已经用过好几次了,效果很好。

PHPExcel: https://github.com/PHPOffice/PHPExcel

【讨论】:

  • 您好,感谢您的建议。但是我的代码现在没有错误,但是它不起作用,我不知道为什么。你能帮忙吗?
  • 嗨,我在 Skype 上给你留言了 :)
  • 好的,我在skype上,请回到skype上的消息,我会调试和更正代码,然后在这里重新发布,
  • 使用 db 模式和 csv 文件的工作示例代码可以从这个链接下载:filedropper.com/workingsample 它工作正常。
猜你喜欢
  • 2015-01-19
  • 2013-12-27
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多