Jump to content

Read text file and insert into mysql


siclines

Recommended Posts

Edit: Longer version of above ^^^

 

Someone is likely to just post the example(s) you have already tried.

 

Perhaps the examples you have tried are not working because your server is configured to prohibit the method you used. You need to find out why the code you tried does not work. Post code you have tried along with any errors or other symptoms you got. Are you attempting this on a system where error_reporting is set to E_ALL and display_errors is set to ON so that php will show you errors that occur in the script?

 

Edit2: I guess I'll ask, which part of this are you having a problem with? Reading the site, parsing the data, or inserting it into a database?

Link to comment
Share on other sites

Edit: also a longer version of the above. :P

 

Which part are you having trouble with? Do you have any code already?

 

Will you be reading the text file from that URL? Do you know how to do that part?

 

Do you know how to parse the text file into the relevant fields ready to insert?

 

Can you do an insert query to the database?

 

I'm reluctant to just write the code for you.

Link to comment
Share on other sites

I am able to read the data from the text file, and I can print the text on the screen, I just can't get it in the db.

 

I tried various examples, I appreaciate everyone's time and I do not expect anyone to write the code for me, just trying to understand what I am doing wrong.

 

I am not getting any errors, just a blank screen.

 

 

Link to comment
Share on other sites

here is the code excluding my connection string:

 

<?php
$file = "http://www.ndbc.noaa.gov/data/5day2/44004_5day.txt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);

$output = str_replace("\t|\t", "|", $data);

$output = explode("\n", $output);


mysql_query("INSERT INTO buoy44 (Year,Month,Day,Hour,Min,WindDir,WindSpeed,Gust,WaveHT,DPD,APD,MWD,Press,Atemp,Wtemp,Dewp,Vis,ptdy,Tide) VALUES('$Year','$Month','$Day','$Hour','$Min','$WindDir','$WindSpeed','$Gust','$WaveHT','$DPD','$APD','$MWD','$Press','$Atemp','$Wtemp','$Dewp','$Vis','$ptdy','$Tide')") or die("Insert failed: " . mysql_error()); 

}
?>

 

(please use


tags when posting code)

Link to comment
Share on other sites

I don't know if you missed some of your code. Where are $Year, $Month etc. and everything you insert being set?

 

Perhaps you need to explode() again, this time by "|", for each line. You need a loop as well, to loop over the array $output. Or have you missed some of your code out?

 

Edit: Actually, looking at the data, there are no pipes ("|") or tabs ("\t") in it, so that won't even work. I think your example code was designed for different data. Your text file looks like it is delimitted only by spaces, which makes it harder.

Link to comment
Share on other sites

Where are all those variables you're inserting coming from?

 

The explode function is exploding the contents of each line of the text file into an array. Do you want to insert each line into its own row in the database? Or does each line contain a uniform formatting that you can use a regular expression on in order to grab each variable from each line?

 

You should really read up on some of these functions at php.net to better understand them, otherwise you're pissing in the wind.

 

edit - I just saw the text file. You can easily use a regular expression to extract the information you want into their own variables.

Link to comment
Share on other sites

Thanks for all your help, I made some changes, but now it only insert a blank record. No data. I read the function explode on php.net, what am I missing?

 

$file = "http://www.ndbc.noaa.gov/data/5day2/44004_5day.txt";

$fp = fopen($file, "r");

$data = fread($fp, filesize($file));

fclose($fp);

 

$output = explode(" ", $output);

 

foreach($output as $var) {

$Year = $Year;

$Month = $Month;

$Day = $Day;

$Hour = $Hour;

$Min = $Min;

$WindDir = $WindDir;

$WindSpeed = $WindSpeed;

$Gust = $Gust;

$WaveHT = $WaveHT;

$DPD = $DPD;

$APD = $APD;

$MWD = $MWD;

$Press = $Press;

$Atemp = $Atemp;

$Wtemp = $Wtemp;

$Dewp = $Dewp;

$Vis = $Vis;

$ptdy = $ptdy;

$Tide = $Tide;

 

$tmp = explode(" ", $var);

 

foreach ($output as $newrow)

mysql_query("INSERT INTO buoy44 (Year,Month,Day,Hour,Min,WindDir,WindSpeed,Gust,WaveHT,DPD,APD,MWD,Press,Atemp,Wtemp,Dewp,Vis,ptdy,Tide) VALUES('$Year','$Month','$Day','$Hour','$Min','$WindDir','$WindSpeed','$Gust','$WaveHT','$DPD','$APD','$MWD','$Press','$Atemp','$Wtemp','$Dewp','$Vis','$ptdy','$Tide')") or die("Insert failed: " . mysql_error());

 

}

Link to comment
Share on other sites

That's not going to work. You're not assigning any values to the variables. Something like this will probably work:

 

<?php
$wx = array_map('trim',file("http://www.ndbc.noaa.gov/data/5day2/44004_5day.txt"));
$newwx = array();
foreach($wx as $i => $line) {
   if ($i > 1) {
        $tmp = array_filter(explode(' ',$line));
        $q = "insert into bouy44 (Year,Month,Day,Hour,Min,WindDir,WindSpeed,Gust,WaveHT,DPD,APD,MWD,Press,Atemp,Wtemp,Dewp,Vis,ptdy,Tide) values ('" . implode("','",$tmp) . "')";
        $rw = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
   }
}
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.