|
|
|
คำสั่ง Import csv หลาย ๆ ไฟล์ที่อยู่ใน folder เดียวกัน ลงใน mysql database ในครั้งเดียว |
|
|
|
|
|
|
|
csv1.csv
1,11,111,1111
1,11,111,1111
1,11,111,1111
csv2.csv
2,22,222,2222
2,22,222,2222
2,22,222,2222
csv3.csv
3,33,333,3333
3,33,333,3333
3,33,333,3333
Code (PHP)
<?
$arr[] = null;
$objScan = scandir("csv");
foreach ($objScan as $value) {
if($value != "." and $value != "..")
{
$strFileName = "csv/".$value;
$objFopen = fopen($strFileName, 'r');
if ($objFopen) {
while (!feof($objFopen)) {
$file = fgets($objFopen, 4096);
$arr[] = $file;
}
fclose($objFopen);
}
}
}
for($i=0;$i<count($arr);$i++)
{
if($arr[$i] != "")
{
$str = explode(",",trim($arr[$i]));
$strSQL = "INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('".$str[0]."','".$str[1]."','".$str[2]."','".$str[3]."') ";
//mysql_query($strSQL);
echo $strSQL."<br>";
}
}
?>
ผลลัพธ์
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('1','11','111','1111')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('1','11','111','1111')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('1','11','111','1111')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('2','22','222','2222')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('2','22','222','2222')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('2','22','222','2222')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('3','33','333','3333')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('3','33','333','3333')
INSERT INTO tb_name (Col1,Col2,Col3,Col4) VALUES ('3','33','333','3333')
|
|
|
|
|
Date :
2011-08-22 14:18:11 |
By :
thaicreate |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|