[英]LOAD DATA INFILE Method for MYSQL in PHP
I want to import data stored in a .csv file into mysql via php. 我想通过php将存储在.csv文件中的数据导入mysql。 The LOAD DATA INFILE query does not seem to work and i managed to write a code that will import the records one by one.
LOAD DATA INFILE查询似乎不起作用,我设法编写了一个将一个接一个地导入记录的代码。 Here is the code:
这是代码:
<?php
$arr = array(array(),array());
$num = 0;
$row = 0;
$handle = fopen("./import.csv", "r");
while($data = fgetcsv($handle,1000,",")){
$num = count($data);
for ($c=0; $c < $num; $c++) {
$arr[$row][$c] = $data[$c];
}
$row++;
}
$con = mysql_connect('localhost','root','password22');
mysql_select_db("security",$con);
for($i=1; $i<$row; $i++){
$sql = "INSERT INTO sls VALUES ('".$arr[$i][0]."','".$arr[$i][1]."','".$arr[$i][2]."','".$arr[$i][3]."','".$arr[$i][4]."','".$arr[$i][5]."')";
mysql_query($sql,$con);
}
?>
The problem is that I have about 300 000 records to import and when the records get too much, no record gets imported into the database and I get an error message. 问题是我要导入大约30万条记录,并且当记录太多时,没有记录导入数据库,并且出现错误消息。 Is there anyway I can import the data faster or are there any similar statements like LOAD DATA INFILE I can use in PHP?
无论如何,我可以更快地导入数据吗?还是可以在PHP中使用类似的类似LOAD DATA INFILE的语句?
This may help you out 这可以帮助您
<?php
require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('filename.xls');
$con=mysqli_connect("localhost","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql="CREATE TABLE tablename(
quote_number VARCHAR(100),
line_no VARCHAR(100),
item_no VARCHAR(100),
name VARCHAR(100),
unit VARCHAR(100),
rm VARCHAR(100),
capex VARCHAR(100))";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table tablename created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
for($x = 2; $x <= count($data->sheets[0]["cells"]); $x++)
{
//$sno = $data->sheets[0]["cells"][$x][1];
$quote_number = $data->sheets[0]["cells"][$x][1];
$line_no = $data->sheets[0]["cells"][$x][2];
$item_no = $data->sheets[0]["cells"][$x][3];
$name = $data->sheets[0]["cells"][$x][4];
$unit = $data->sheets[0]["cells"][$x][5];
$rm = $data->sheets[0]["cells"][$x][6];
$capex = $data->sheets[0]["cells"][$x][7];
$res = mysqli_query($con,"INSERT INTO tablename
(quote_number, line_no, item_no,name,unit,rm,capex) VALUES ('$quote_number','$line_no','$item_no','$name','$unit','$rm','$capex')");
mysqli_close($res);
}
?>
you can download reader.php file from here 您可以从这里下载reader.php文件
Since you have not shared the LOAD DATA INFILE
I assume most probably the issue is with FILE Privileges 由于您尚未共享
LOAD DATA INFILE
我认为问题很可能与FILE Privileges有关。
You have to GRANT FILE privileges to the person loading the file(Mysql user connecting through username/password from php). 您必须授予加载文件的人FILE特权(Mysql用户通过php的用户名/密码连接)。 Sth like this:
像这样:
GRANT FILE on *.* TO 'mysql_user'@'localhost' ;
Since FILE
is global privilege you cannot localize that to a specific database eg dbname.* , just to note. 由于
FILE
是全局特权,因此请注意,您不能将其本地化到特定数据库,例如dbname。*。 To run the above command itself you should login to mysql from command line as root
or as a user who has GRANT
privileges. 要运行上面的命令本身,您应该以
root
或具有GRANT
特权的用户身份从命令行登录mysql。
Once that is done you should able to load the file using LOAD DATA INFILE
. 完成后,您应该能够使用
LOAD DATA INFILE
加载文件。 IF you are trying this in shared-hosting environment, your chances are very little. 如果您在共享主机环境中尝试此操作,则机会很小。 In that case you have to use the above method you tried , read the file, split the each line ,validate the line , insert into db table.
在这种情况下,您必须使用上面尝试过的方法,读取文件,分割每一行,验证该行,然后插入db表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.