Help me to understand how to fetch this text file

OK here is the url

ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KEMT.TXT

very simple text file. I need to come up with php code that goes to this url and logs this text every hour.
Text need to be inserted into mysql database , top data and time on text file and bottom part into another

how can i do it please help

This is how you can get the two lines in two variables. Do you know how to do an SQL insert or do you need help with that as well?

[php]

<? $lines = file('ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KEMT.TXT'); $top_line = $lines[0]; $bottom_line = $lines[1]; ?>

[/php]

oh and to run this script every hour you will need a ‘Cron Job’, I cant describe how to do this very well so I will point you towards… http://www.thesitewizard.com/general/set-cron-job.shtml

Unless you are guaranteed at least one visiter an hour, then you can include this script on your index.php page and put an if statement in to check if it has been an hour since the script was last run, and if so then run it…

Yes i need the script on putting this into mysql database. Thanks in advance,

I know the cron job part. I just need to put this into mysql database

ok say the table is called “tbl_example” and it has columns “top_line” and “bottom_line”

[php]

<? $lines = file('ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KEMT.TXT'); $top_line = $lines[0]; $bottom_line = $lines[1]; INSERT INTO tbl_example (`top_line`, `bottom_line`) VALUES("$top_line","$bottom_line"); ?>

[/php]

should be what you need

sorry mate, missed the mysql_query part of course…
[php]
mysql_query(“INSERT INTO tbl_example (top_line, bottom_line) VALUES(’$top_line’,’$bottom_line’)”);
[/php]

How about mysql database login and stuff? I’m planning to put this code on my hosting and also setup cron job. I try running first code and nothing happened. Can you put complete code here. I’m very new sorry

[php]

$dbhost = “…”; //the name of your host, for example “example.com
$dbuser = “…”; //your database username
$dbpass = “…”; //your database password
$dbname = “…”; //the name of the database you are accessing

//this connects to mysql
$dbconnection=mysql_connect($dbhost, $dbuser, $dbpass);

//if it cannot make a connection then the mysql error is shown
if (!$dbconnection) {
die('Could not connect: ’ . mysql_error());
}

//this selects the correct database
$db_selected=mysql_select_db($dbname, $dbconnection);

$lines = file(‘ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KEMT.TXT’);

$top_line = $lines[0];
$bottom_line = $lines[1];

mysql_query(“INSERT INTO tbl_example (top_line, bottom_line) VALUES(’$top_line’,’$bottom_line’)”);

[/php]

Didn’t work. I created databases and stuff but nothing in the database

Oh it did add only top part till 2010

Hey maas do you mind adding me to your MSN. Mine is b e s t b u y 4 u s “@” h o t m a i l . c o m

sorry mate I dont use MSN. I have just set up a database with those headings and run the script myself, and for me it works perfectly. Check your two columns are VARCHAR and to be sure put size as 400, double check that the names of the columns are the same in your script.

my database columns in tbl_example
top_line || VARCHAR(400)
bottom_line || VARCHAR(400)

I did

CREATE TABLE aerofox_metar.tbl_example (
top_line VARCHAR( 400 ) NOT NULL ,
bottom_line VARCHAR( 400 ) NOT NULL
) ENGINE = MYISAM ;

I’ll be testing soon.
We’ll see what will happen

It works! Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service