假设您从源接收数据并准备此数组:
$x=array(1=>array('1','2','3','4'), 2=>array('5','6','7','8'));
创建一个这样的csv文件:
$file=fopen(<DIRECTORY_PATH>."file.csv","w");
if(!$file){
//error
}
$csv_data="";
foreach($x as $row){
foreach($row as $element){
$csv_data.=$element.", ";
}
//remove the last comma
$csv_data.="\n";
}
fwrite($file,$csv_data);
$query="load data infile '".<DIRECTORY_PATH>."file.csv"."' into table your_table";
if(!mysqli->query($query))
printf("Error: %s\n", $mysqli->error);
assume you receive data from your source and prepare this array:
$x=array(1=>array('1','2','3','4'), 2=>array('5','6','7','8'));
create a csv file like this:
$file=fopen(<DIRECTORY_PATH>."file.csv","w");
if(!$file){
//error
}
$csv_data="";
foreach($x as $row){
foreach($row as $element){
$csv_data.=$element.", ";
}
//remove the last comma
$csv_data.="\n";
}
fwrite($file,$csv_data);
$query="load data infile '".<DIRECTORY_PATH>."file.csv"."' into table your_table";
if(!mysqli->query($query))
printf("Error: %s\n", $mysqli->error);
请发表评论