Php Mysql将多个insert语句转换为一个语句
我有一个脚本可以上传一个excel文件,并将数据从xlsx文件插入mysql表。是这样的Php Mysql将多个insert语句转换为一个语句,php,mysql,excel,Php,Mysql,Excel,我有一个脚本可以上传一个excel文件,并将数据从xlsx文件插入mysql表。是这样的 <?php require_once('Connections/met.php'); $file = './uploads/windrose_data.xlsx'; if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { $msg="File upload successful"; $db=mysql_selec
<?php
require_once('Connections/met.php');
$file = './uploads/windrose_data.xlsx';
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
$msg="File upload successful";
$db=mysql_select_db($database_met,$met);
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
// This is the file path to be uploaded.
$inputFileName = $file;
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
for($i=2;$i<=$arrayCount;$i++){
$date = trim($allDataInSheet[$i]["A"]);
$time = trim($allDataInSheet[$i]["B"]);
$dir = trim($allDataInSheet[$i]["C"]);
$spd = trim($allDataInSheet[$i]["D"]);
$insertTable= mysql_query("insert into wr_copy (date,time,dir,spd) values('$date', '$time',$dir,$spd)") or die(mysql_error());
$msg=$i-1." records inserted into the table";
}
echo $msg;
} else {
echo "Upload Failed";
}
?>
创建一条语句的
:
$statement = 'insert into wr_copy (date,time,dir,spd) values';
$values = [];
for($i=2;$i<=$arrayCount;$i++){
$date = trim($allDataInSheet[$i]["A"]);
$time = trim($allDataInSheet[$i]["B"]);
$dir = trim($allDataInSheet[$i]["C"]);
$spd = trim($allDataInSheet[$i]["D"]);
$values[] = "('$date', '$time',$dir,$spd)";
}
$statement .= implode(',',$values);
要创建one语句,请执行以下操作:
$statement = 'insert into wr_copy (date,time,dir,spd) values';
$values = [];
for($i=2;$i<=$arrayCount;$i++){
$date = trim($allDataInSheet[$i]["A"]);
$time = trim($allDataInSheet[$i]["B"]);
$dir = trim($allDataInSheet[$i]["C"]);
$spd = trim($allDataInSheet[$i]["D"]);
$values[] = "('$date', '$time',$dir,$spd)";
}
$statement .= implode(',',$values);
请参阅:作为旁白,请查看从mysql.*
切换到mysqli
或PDO
mysql.*
已被弃用。另外,请注意使用准备好的语句而不是字符串连接参见:另外,请注意从mysql.*
切换到mysqli
或PDO
mysql.*
已被弃用。此外,还可以研究使用准备好的语句而不是字符串串联$values=[];标记了一个错误,我将其更改为$values=array();哦,是的,对于php<5.4,您需要这样更改它。sry$值=[];标记了一个错误,我将其更改为$values=array();哦,是的,对于php<5.4,您需要这样更改它。sry