/    Sign up×
Community /Pin to ProfileBookmark

Here’s a Challenge! Parse and insert to MySQL

I am trying to parse a current PHP file, with basic data using white space as a delimiator and place that data into a MySQL database.

Here is what I have, can anyone tell me where I am going wrong?

The script connects fine, and actually inserts a record into the database, only problem is, the record is blank.

All files are local on, [url]www.metrosurfing.com/xml/noaa/[/url]

[CODE]

<?php

// THIS FILE GRABS LATEST BUOY DATA FROM THE BUOY_DATA.PHP file and SHOVES IT IN A DATABASE

// Database Information
$host = “localhost”;
$user = “metrosurfer”;
$pass = “metropass1961”;
$db = “cid25519_buoy”;
$tbl = “bouy_data”;

$connection = mysql_connect($host, $user, $pass) or die (“Unable to connect!”);
mysql_select_db($db) or die (“Unable to select database!”);

// Open File
$filename = “buoy_data.php”;
$fp = fopen($filename, “r”) or die(“What happend? The file wont open, ****er! $filename”);

// Parse
$count = 0;
$STN = “”;
$YY = “”;
$MM = “”;
$DD = “”;
$hh = “”;
$min = “”;
$WDIR = “”;
$WSPD = “”;
$WVHT = “”;
$DPD = “”;
$APD = “”;
$MWD = “”;
$ATMP = “”;
$WTMP = “”;

while (!feof($fp)){
$string = fgets($fp, 1024);
$string2 = str_replace(“,”, “”, $string);
$count = count(explode(” “, $string2));
$values = explode(” “, $string2);
$STN = $values[0];
$YY = $values[1];
$MM = $values[2];
$DD = $values[3];
$hh = $values[4];
$min = $values[5];
$WDIR = $values[6];
$WSPD = $values[7];
$WVHT = $values[8];
$DPD = $values[9];
$APD = $values[10];
$MWD = $values[11];
$ATMP = $values[12];
$WTMP = $values[13];

}

// insert into databse
mysql_query(“inSERT into bouy_data (STN, YY, MM, DD, hh, min, WDIR, WSPD, WVHT, DPD, APD, MWD, ATMP, WTMP) VALUES(‘$STN’, ‘$YY’, ‘$MM’, ‘$DD’, ‘$hh’, ‘$min’, ‘$WDIR’, ‘$WSPD’, ‘$WVHT’, ‘$DPD’, ‘$APD’, ‘$MWD’, ‘$ATMP’, ‘$WTMP’) “) or die(mysql_error());

echo “shove completed.”

?>

[/CODE]

Thank you!!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@tcaJul 31.2007 — $string2 = str_replace(",", "", $string); It appears you are replacing a comma with nothing.

$count = count(explode(" ", $string2)); Then you are exploding on a whitespace

$values = explode(" ", $string2); Again, a whitespace

Why can't you just explode on the comma and trim the output? Can't tell without looking at the raw data. That link above gives me a 403 Forbidden error.

TC
×

Success!

Help @metrouni spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...