我已经搜索了关于这个问题的最后一个小时,但它变得非常烦人......
无论如何,我正在尝试将数据插入到 mysqli 数据库中,我之前已经做过很多次但由于某种原因我无法弄清楚出了什么问题......
<?php
require_once("../config/dbmetar.php");
$delimiter = ',';
$file = "metars.csv";
$db = new mysqli(DB_HOST_METAR, DB_USER_METAR, DB_PASS_METAR, DB_NAME_METAR);
$r = 0;
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 5000, $delimiter)) !== FALSE) {
if ($r >= 6) {
foreach($data as $i => $content) {
$data[$i] = $db->real_escape_string($content);
}
echo "INSERT INTO metars VALUES('" . implode("','", $data) . "');" . "<br>";
//$db->query("INSERT INTO metars VALUES('" . implode("','", $data) . "');" );
}
$r++;
}
fclose($handle);
}
?>
主要问题是插入metars值......这些值都应该用逗号分隔,每个字段值周围都有单引号......
目前这是 echo 正在导出的内容:
INSERT INTO metars VALUES('PAJZ 011132Z AUTO 1 3/4SM BR FEW009 BKN019 OVC026 08/07 A2959 RMK AO2 PWINO TSNO P0001,PAJZ,2013-07-01T11:32:00Z,59.73,-157.27,8.0,7.0,,,,1.75,29.589567,,,TRUE,TRUE,,,TRUE,,TRUE,BR,FEW,900,BKN,1900,OVC,2600,,,IFR,,,,,,0.01,,,,,,SPECI,82.0');
有什么建议么?
Var_dump($data) 返回了这些:
array(1) { [0]=> string(238) "PAJZ 011132Z AUTO 1 3/4SM BR FEW009 BKN019 OVC026 08/07 A2959 RMK AO2 PWINO TSNO P0001,PAJZ,2013-07-01T11:32:00Z,59.73,-157.27,8.0,7.0,,,,1.75,29.589567,,,TRUE,TRUE,,,TRUE,,TRUE,BR,FEW,900,BKN,1900,OVC,2600,,,IFR,,,,,,0.01,,,,,,SPECI,82.0" } INSERT INTO metars VALUES('');
array(1) { [0]=> string(153) "CYOD 011131Z 18002KT 1/2SM FG FEW250 RMK CI0,CYOD,2013-07-01T11:31:00Z,54.4,-110.28,,,180,2,,0.5,,,,,,,,,,,FG,FEW,25000,,,,,,,LIFR,,,,,,,,,,,,SPECI,544.0" } INSERT INTO metars VALUES('');
array(1) { [0]=> string(276) "CYYD 011131Z AUTO VRB06KT 5SM -RA BR FEW007 BKN070 OVC084 15/14 A3005 RMK PRESRR PCPN 1.0MM PAST HR SLP173 DENSITY ALT 1900FT,CYYD,2013-07-01T11:31:00Z,54.82,-127.18,15.0,14.0,0,6,,5.0,30.050198,1017.3,,TRUE,,,,,,,-RA BR,FEW,700,BKN,7000,OVC,8400,,,MVFR,,,,,,,,,,,,SPECI,523.0" }