在PHP中将CSV文件导入Mysql数据库

时间:2016-09-14 03:22:30

标签: php mysql csv import upload

我尝试使用LOAD DATA LOCAL INFILE从其他服务器导入csv文件,但它给了我一个错误The user update failed: Can't find file,即使该文件存在于给定的路径中。我还尝试了LOAD DATA INFILEUser access denied

是否还有其他方法可以将csv文件直接导入mysql数据库中的文件路径(不使用任何提交按钮),如果有新的csv,则删除dbase表中的当前数据文件已上传?

编辑:我也试过这个PHP脚本,导入csv文件时没有任何按钮,但是csv文件数据没有导入

PHP(第二代码):

$mysqli = new mysqli("localhost", "user", "password", "dbasename");
$file = "/var/www/html/files/filename.csv";
$handle = fopen($file,'r');
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE){
     $num = count($data);
     for ($c=0; $c < $num; $c++){
         $col[$c] = $data[$c];
     }

     $col1 = $data[0];
     $col2 = $data[1];
     $col3 = $data[2];
     $col4 = $data[3];
     $col5 = $data[4];

     // SQL Query to insert data into DataBase
     $query = "INSERT INTO dbname.tablename(col1,col2,col3,col4,col5) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."')";
     $mysqli->query($query);
}
fclose($handle);

这是PHP:

$sql = "LOAD DATA LOCAL INFILE 'filename.csv'
       INTO TABLE dbname.tablename
       FIELDS TERMINATED BY ','
       OPTIONALLY ENCLOSED BY '\"' 
       LINES TERMINATED BY '\n' 
       IGNORE 1 LINES;";

$con=mysqli_connect("localhost","user","password","db");
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
};

$result = mysqli_query($con, $sql);

if (mysqli_affected_rows($con) == 1){
  $message = "The data was successfully added!";
} else {
  $message = "The user update failed: ";
  $message .= mysqli_error($con); 
};

echo $message;

3 个答案:

答案 0 :(得分:0)

之前已经提出并回答了这个问题。更好地使用谷歌: Mysql permission errors with 'load data'

您需要向mysql用户授予文件权限。

答案 1 :(得分:0)

您也可以尝试使用

$con=mysqli_connect("localhost","user","password","db");
$file = fopen("filename.csv","r");
while ($data = fgetcsv ($file)){
     print_r($data);
     $sql = 'insert into ...'
     $result = mysqli_query($con, $sql);
}

导入csv文件

答案 2 :(得分:0)

<?php 
	$connect = mysqli_connect("localhost", "root", "", "testing");
	if(isset($_POST["submit"]))
{
	if($_FILES['file']['name'])
	{
		$filename = explode(".", $_FILES['file']['name']);
		if($filename[1] == 'csv')
		{
			$handle = fopen($_FILES['file']['tmp_name'], "r");
			while($data = fgetcsv($handle))
			{
				$item1 = mysqli_real_escape_string($connect, $data[0]);  
				$item2 = mysqli_real_escape_string($connect, $data[1]);
				$query = "INSERT into excel(excel_name, excel_phone) values('$item1','$item2')";
				mysqli_query($connect, $query);
			}
			fclose($handle);
			echo "<script>alert('Import done');</script>";
		}
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
	<style>
		table{
			border-collapse: collapse;
			width: 100%;
			color: #588c7e;
			font-family: monospace;
			font-size: 25px;
			text-align: left;
		}
		th{
			background-color: #588c7e;
			color: white;
		}
		tr:nth-child(even) {background-color: #f2f2f2}
	</style>
</head>
<body>
	<form method="post" enctype="multipart/form-data">
		<div align="center">
			<label>Select CSV File:</label>
			<input type="file" name="file" />
			<br />
			<input type="submit" name="submit" value="Import" class="btn btn-info" />
		</div>
	</form><br><br>
  </body>
  </html>