BackNext

PHP

Now to the PHP file which receives the data and puts it into the database. There are probably quite a few ways that this could be done. Perhaps the simplest way is just to use the $_GET method. PHP sees everything after a "?" in a URL as a query string, and uses "&" as a delimiter. So all of the hard work is automatically done by PHP. Just open the database, select the table, and input the data using $_GET like this.

<?php
$con = mysql_connect("host_name","user_name","password");
  if (!$con)
 { 
die('Could not connect: ' . mysql_error());
 } 
mysql_select_db("database_name", $con);

$sql="INSERT INTO table_name (tm, t1, t2, dp, rh, bp, wdspd, gst, wddir, rn, drn, rnrt) VALUES ('$_GET[tm]','$_GET[t1]','$_GET[t2]','$_GET[dp]','$_GET[rh]','$_GET[bp]','$_GET[wdspd]','$_GET[gst]','$_GET[wddir]','$_GET[rn]','$_GET[drn]','$_GET[rnrt]')";
 if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 } 
mysql_close($con)
?>

This method is so easy it is scary. But it does not allow for making tests on the data that might need to be preformed. So I made a different script, similar, but a bit more versatile. This is the one I am using now, it is called wxupdate.php. You can download the code here. It is commented, and should be easy to implement for anyone with a basic understanding of PHP.

<?php
/*
Title = wxupdate.php

Usage = Script to input data supplied by OWW to mySQL database.
Author = David Dean
Last modified = 03/07/2008

Free to use or modify however you need.
No warranty express or implied. If you use this script, 
I would appreciate a link back to deanostoybox.com.
*/

// Setup variables for database

$host = "localhost" ; //default chance to your mysql hostname if different.
$user = "user_name" ; //your database user name
$pass = "password" ; //your database password
$db = "database_name"; //name of your database
$dbf = "table_name" ; //name of database field

// Parse query string from URL and put into an array
// This part fetches the part after the "?" from the URL.
$data = "$_SERVER[QUERY_STRING]";

// echo "$data <br />"; //test returns query string

//Break query string into parts delimited by the "&" (key=val)

$query_string = explode( '&', $data );
// echo "$query_string <br />"; //test returns "Array"

$args = array( ); // return array
// echo "$args <br />" //test returns "Array"

//Loop through the array, break at "=",

foreach( $query_string as $chunk )
{
$chunk = explode( '=', $chunk );

list( $key, $val ) = $chunk;

// echo "$key $val <br />"; //test returns keys and their assosiated values

/*
****************************************************
For each iteration of the loop test for key, create 
a variable and set its value to the one associated with it.
These values will be used later on to update the database. 
If you have any different sensor values than the ones here, 
you need to create your own test.
 Format is-
"case ($key == "your_parameter"): //If the key matches returns true
global $your_parameter; //declare parameter as global variable
$your_parameter = $val; //set the value
break;" //Back to the loop
***************************************************
*/

switch ($key):

case ($key == "tm"):
global $tm;
$tm = $val;
break;

case ($key == "t1"):
global $t1;
$t1 = $val;
break;

case ($key == "t2"):
global $tm;
$t2 = $val;
break;

case ($key == "dp"):
global $dp;
$dp = $val;
break;

case ($key == "rh"):
global $rh;
$rh = $val;
break;

case ($key == "bp"):
global $bp;
$bp = $val;
break;

case ($key == "wdspd"):
global $wdspd;
$wdspd = $val;
break;

case ($key == "gst"):
global $gst;
$gst = $val;
break;

case ($key == "wddir"):
global $wddir;
$wddir = $val;
break;

case ($key == "rn"):
global $rn;
$rn = $val;
break;

case ($key == "drn"):
global $drn;
$drn = $val;
break;

case ($key == "rnrt"):
global $rnrt;
$rnrt = $val;
break;

endswitch;
$args[ $key ] = urldecode( $val );
}
// connect to the database
$con = mysql_connect("$host","$user","$pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// select the database
mysql_select_db("$db", $con) or die ("error selecting db");
// Insert values into the fields in the database
mysql_query("INSERT INTO $dbf (tm, t1, t2, dp, rh, bp, wdspd, gst, wddir, rn, drn, rnrt) VALUES ('$tm','$t1','$t2','$dp','$rh','$bp','$wdspd','$gst','$wddir','$rn','$drn','$rnrt')");

mysql_close($con); //close the database
// echo "$tm $t1 $t2 $dp $rh $bp $wdspd $gst $wddir $rn $drn $rnrt"; //test to see if created variables get this far
?>

Then all that is needed is to have a script to test the database to see if it is working. That can be downloaded here. The code is below. This is only useful to see that the data is getting into the database. After a couple of hours it will overload a browser, so you would only want to use a script like this once or twice early on. Now you know the data is getting there and can be accessed, it is time to write some scripts to make use of the data.

<?php
$con = mysql_connect("host_name","user_name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database_name", $con);

$result = mysql_query("SELECT * FROM table_name");

while($dd = mysql_fetch_array($result))
{
echo $dd['tm'], $dd['t1'], $dd['t2'], $dd['dp'], $dd['rh'], $dd['bp'], $dd['wdspd'], $dd['gst'], $dd['wddir'], $dd['rn'], $dd['drn'], $dd['rnrt'];
echo "<br />";
}

mysql_close($con);
?>

BackNext