我的 fgetcsv 命令似乎不是逗号分隔的问题,基本上发生的事情是我的数据只是数组中的一个非常长的字符串。我需要数据采用数组格式,以便内爆函数正常工作以上传到 mysql 数据库。
目前mysql上传看起来像......
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"
这应该在每个元素周围有单引号,但是由于它是一个字符串,因此内爆不起作用=\
<?php
require_once("../config/dbmetar.php");
$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, 3000, ",", '"')) !== FALSE) {
if ($r >= 6) { //skips the header
foreach($data as $i => $content) {
$data[$i] = $db->real_escape_string($content);
}
//echo "INSERT INTO metars VALUES('" . implode("','", $data) . '"' ;
//echo var_dump($data);
$db->query("INSERT INTO metars VALUES('" . implode("','", $data) . "');");
}
$r++;
}
fclose($handle);
}
?>
这是 csv 文件的示例
No errors
Max results hit (1000): not all possible results returned;
59 ms
data source=metars
948 results
raw_text,station_id,observation_time,latitude,longitude,temp_c,dewpoint_c,wind_dir_degrees,wind_speed_kt,wind_gust_kt,visibility_statute_mi,altim_in_hg,sea_level_pressure_mb,corrected,auto,auto_station,maintenance_indicator_on,no_signal,lightning_sensor_off,freezing_rain_sensor_off,present_weather_sensor_off,wx_string,sky_cover,cloud_base_ft_agl,sky_cover,cloud_base_ft_agl,sky_cover,cloud_base_ft_agl,sky_cover,cloud_base_ft_agl,flight_category,three_hr_pressure_tendency_mb,maxT_c,minT_c,maxT24hr_c,minT24hr_c,precip_in,pcp3hr_in,pcp6hr_in,pcp24hr_in,snow_in,vert_vis_ft,metar_type,elevation_m"
"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"
"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"
"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"
"KNSE 011131Z 00000KT 10SM -RA FEW025 BKN070 BKN120 BKN250 23/19 A2980 RMK AO2 WSHFT 1045 RAB29 P0000 $ ,KNSE,2013-07-01T11:31:00Z,30.72,-87.02,23.0,19.0,0,0,,10.0,29.799213,,,,TRUE,TRUE,,,,,-RA,FEW,2500,BKN,7000,BKN,12000,BKN,25000,VFR,,,,,,0.0050,,,,,,SPECI,61.0"
我一直在尝试解决这个难题大约 6 个小时,但没有运气,我尝试了不同的 csv 拉取方法,例如 str_getcsv、fgetcsv,我尝试了 foreach 和 for... 简化版本是我在下面并且是更新标头信息的最简单方法。
目的:这个 php 脚本将在 cron 调度程序中每 60 秒到 150 秒运行一次,如果您对此有任何其他建议,我也将不胜感激。
我非常感谢在这件事上的任何帮助,
谢谢,
-米凯尔