澳门新葡亰娱乐官网php中数据的批量导入(csv文件)

有时写程序时后台供给把大气数额导入数据库中,比方Computer考试战绩的查询、电话簿的多少等日常都是存放在在excel中的,这个时候我们可把数量导出成csv文件,然后通过以下顺序就能够在后台批量导入数据到数据库中。

下边只是入眼程序部分:

上面只是关键程序部分:

 代码如下

?php
/***************************************************作者:冲星/arcow**************************njj@nuc.edu.cn*****************************php导入csv文件到数据库********************再正是总括程序实行时间************************************************************************///定义获取时间函数
function getmicrotime(卡塔尔国{ list($usec, $sec卡塔尔(قطر‎ = explode(” “,microtime(卡塔尔State of Qatar;
return ((floatState of Qatar$usec + (float卡塔尔国$secState of Qatar; }?

<?php
/*****************************************
**********作者:冲星/arcow****************
**********njj@nuc.edu.cn*******************
**********php导入csv文件到数据库**********
**********再正是计算程序实践时间***********
**********www.111cn.net***********
****************************************/
//定义获取时间函数
function getmicrotime(){
    list($usec, $sec) =
explode(” “,microtime());
    return ((float)$usec + (float)$sec);
}
?>

?php$time_start =
getmicrotime(卡塔尔国;include(“db.inc.php”卡塔尔;//连接数据库$db=new
testcsv;??php$handle = fopen (“test.csv”,”r”卡塔尔(قطر‎;$sql=”insert into
scores(idcard,names,num,sex,nation,scoreState of Qatar values(‘”;while ($data =
fgetcsv ($handle, 1000, “,”卡塔尔卡塔尔国 { $num = count ($dataState of Qatar; for ($c=0; $c
$num; $c++卡塔尔(قطر‎ { if($c==$num-1卡塔尔国{$sql=$sql.$data[$c].”‘)”;break;}
$sql=$sql.$data[$c].”‘,'”; }print “br”;echo
$sql.”br”;$db-query($sql卡塔尔(قطر‎;echo “SQL语句实践成功!br”;$sql=”insert into
scores(idcard,names,num,sex,nation,scoreState of Qatar values(‘”;}fclose
($handle卡塔尔国;$time_end = getmicrotime();$time = $time_end –
$time_start;echo “程序实施时间:”.$time.”秒”;?

<?php
$time_start = getmicrotime();
include(“db.inc.php”);//连接数据库
$db=new testcsv;
?>

<?php
$handle = fopen
(“test.csv”,”r”);
$sql=”insert into scores(idcard,names,num,sex,nation,score) values(‘”;
while ($data = fgetcsv ($handle, 1000, “,”)) {
   $num = count ($data);
   for ($c=0; $c < $num; $c++) {
       if($c==$num-1){$sql=$sql.$data[$c].”‘)”;break;}
       $sql=$sql.$data[$c].”‘,'”;
   }
print “<br>”;
echo $sql.”<br>”;
$db->query($sql);
echo “SQL语句施行成功!<br>”;
$sql=”insert into scores(idcard,names,num,sex,nation,score) values(‘”;
}
fclose ($handle);
$time_end = getmicrotime();
$time = $time_end – $time_start;
echo “程序施行时间:”.$time.”秒”;
?>

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图