Question

I seem to be having a problem with my fgetcsv command not comma delimiting, basically what is happening is my data is just one really long string in an array. I need the data to be in an array format for the implode function to work properly for uploading into a mysql database.

Currently the mysql upload looks like...

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"

This should have single quotes around each element, however since its a string the implode isn't working =\

<?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);
}

?>

Here's an example of the csv file

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"

I've been trying to solve this dilemma for about 6 hours with no luck, i've tried different csv pull methods such as str_getcsv, fgetcsv, i've tried both foreach and for... the simplified version is what I have below and is the easiest way for updating header information.

Purpose: this php script will be ran about every 60sec-150sec in a cron scheduler, if you have any other suggestions regarding this, I would appreciate that information as well.

I do appreciate any help in this matter,

Thanks,

-Mikael

Was it helpful?

Solution

You might want to consider using file() for this type of operation so you can trim the first few lines out and trim the quotes off the ends of the CSV string. The fgetcsv parameters you passed are for each column to be wrapped in quotes, not the entire string.

From there, you can then use str_getcsv()

Example (untested):

<?php
$file = file("metars.csv");
for($i = 0; $i < 6; $i++) array_shift($file);
foreach($file as $line) {
  $line = ltrim($line, '"');
  $line = rtrim($line, '"');
  $csv  = str_getcsv($line);
  /** do query, preferably a prepared statement **/
}
?>

Of course, if your file is massive - this may not be the best solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top