Tissy
10-01-2011, 01:12 AM
Hi,
I have a small piece of code which updates a mySQL database using PHP. The values the database are updated with come from a microcontroller.
Not being particularly familiar with PHP, is there a way that when the values are updated, it also stores these values in a xml document with a structure similar to below.
Then when the new set of values are updated, the xml is overwritten with the new values.
<?xml version="1.0"?>
-<data>
-<item>
<time>01/10:14am</time>
<temperature>10.5</temperature>
<humidity>86</humidity>
</item>
</data>
<?php
// connect to MySQL
mysql_connect('xxxxx','xxxxx','xxxxx') or die("Can't connect that way!");
@mysql_select_db('arduino') or die("Unable to select a database called 'Arduino'");
if(ISSET($_GET['t']) && (is_numeric($_GET['t'])) && $_SERVER['REMOTE_ADDR']=='192.168.1.xxx'){
// message from the Arduino
$temp = $_GET['t'];
$humidity = $_GET['h'];
// now - 60sec x 60min x 24hrs = 1 day ago
$one_day_ago = time() + 60*60*24;
$qry = "INSERT INTO temp(timing, temp, humidity) VALUES(".time().",'$temp','$humidity')";
$qry_del = "DELETE FROM temp WHERE timing < UNIX_TIMESTAMP(NOW() - INTERVAL 3 DAY)";
mysql_query($qry);
mysql_query($qry_del);
// OPTIMIZE TABLE temp
mysql_close();
exit('200');
}
mysql_close();
?>
Any help and code examples appriciated.
Steve
I have a small piece of code which updates a mySQL database using PHP. The values the database are updated with come from a microcontroller.
Not being particularly familiar with PHP, is there a way that when the values are updated, it also stores these values in a xml document with a structure similar to below.
Then when the new set of values are updated, the xml is overwritten with the new values.
<?xml version="1.0"?>
-<data>
-<item>
<time>01/10:14am</time>
<temperature>10.5</temperature>
<humidity>86</humidity>
</item>
</data>
<?php
// connect to MySQL
mysql_connect('xxxxx','xxxxx','xxxxx') or die("Can't connect that way!");
@mysql_select_db('arduino') or die("Unable to select a database called 'Arduino'");
if(ISSET($_GET['t']) && (is_numeric($_GET['t'])) && $_SERVER['REMOTE_ADDR']=='192.168.1.xxx'){
// message from the Arduino
$temp = $_GET['t'];
$humidity = $_GET['h'];
// now - 60sec x 60min x 24hrs = 1 day ago
$one_day_ago = time() + 60*60*24;
$qry = "INSERT INTO temp(timing, temp, humidity) VALUES(".time().",'$temp','$humidity')";
$qry_del = "DELETE FROM temp WHERE timing < UNIX_TIMESTAMP(NOW() - INTERVAL 3 DAY)";
mysql_query($qry);
mysql_query($qry_del);
// OPTIMIZE TABLE temp
mysql_close();
exit('200');
}
mysql_close();
?>
Any help and code examples appriciated.
Steve