1. The answer to your question - UPDATE
With rows:
<?php
mysql_connect("000.000.000.000", "xxxx", "xxxx") or die(mysql_error());
mysql_select_db("arduino_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM `weather` order by `add` desc limit 1") or die(mysql_error());
$xml = new DOMDocument( "1.0", "UTF-8" );
while($info = mysql_fetch_array( $data ))
{
$row = $xml->createElement( 'row' . $i++ . '' );
$xml_row->appendChild( $row );
$currentDirection = $xml->createElement( 'currentDirection', '' . $info['currentDirection'] . '' );
$xml_row->appendChild( $currentDirection );
$light = $xml->createElement( 'light', '' . $info['light'] . '' );
$xml_row->appendChild( $light );
$pressure = $xml->createElement( 'pressure', '' . $info['pressure'] . '' );
$xml_row->appendChild( $pressure );
$humidity = $xml->createElement( 'humidity', '' . $info['humidity'] . '' );
$xml_row->appendChild( $humidity );
$tempC = $xml->createElement( 'tempC', '' . $info['tempC'] . '' );
$xml_row->appendChild( $tempC );
$rainin = $xml->createElement( 'rainin', '' . $info['rainin'] . '' );
$xml_row->appendChild( $rainin );
$windSpeed = $xml->createElement( 'windSpeed', '' . $info['windSpeed'] . '' );
$xml_row->appendChild( $windSpeed );
}
$xml_file_contents = $xml->saveXML();
$filename = 'your_xml_file_name.xml';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $xml_file_contents will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $xml_file_contents to our opened file.
if (fwrite($handle, $xml_file_contents) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($xml_file_contents) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
1. The answer to your question
<?php
mysql_connect("000.000.000.000", "xxxx", "xxxx") or die(mysql_error());
mysql_select_db("arduino_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM `weather` order by `add` desc limit 1") or die(mysql_error());
$xml = new DOMDocument( "1.0", "UTF-8" );
while($info = mysql_fetch_array( $data ))
{
$currentDirection = $xml->createElement( 'currentDirection', '' . $info['currentDirection'] . '' );
$xml->appendChild( $currentDirection );
$light = $xml->createElement( 'light', '' . $info['light'] . '' );
$xml->appendChild( $light );
$pressure = $xml->createElement( 'pressure', '' . $info['pressure'] . '' );
$xml->appendChild( $pressure );
$humidity = $xml->createElement( 'humidity', '' . $info['humidity'] . '' );
$xml->appendChild( $humidity );
$tempC = $xml->createElement( 'tempC', '' . $info['tempC'] . '' );
$xml->appendChild( $tempC );
$rainin = $xml->createElement( 'rainin', '' . $info['rainin'] . '' );
$xml->appendChild( $rainin );
$windSpeed = $xml->createElement( 'windSpeed', '' . $info['windSpeed'] . '' );
$xml->appendChild( $windSpeed );
}
$xml_file_contents = $xml->saveXML();
$filename = 'your_xml_file_name.xml';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $xml_file_contents will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $xml_file_contents to our opened file.
if (fwrite($handle, $xml_file_contents) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($xml_file_contents) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
2. More about writing XML
files
// "Create" the document.
$xml = new DOMDocument( "1.0", "ISO-8859-15" ); // or 'UTF-8'
// Create some elements.
$xml_album = $xml->createElement( "Album" );
$xml_track = $xml->createElement( "Track", "The ninth symphony" );
// Set the attributes.
$xml_track->setAttribute( "length", "0:01:15" );
$xml_track->setAttribute( "bitrate", "64kb/s" );
$xml_track->setAttribute( "channels", "2" );
// Create another element, just to show you can add any (realistic to computer) number of sublevels.
$xml_note = $xml->createElement( "Note", "The last symphony composed by Ludwig van Beethoven." );
// Append the whole bunch.
$xml_track->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
// Repeat the above with some different values..
$xml_track = $xml->createElement( "Track", "Highway Blues" );
$xml_track->setAttribute( "length", "0:01:33" );
$xml_track->setAttribute( "bitrate", "64kb/s" );
$xml_track->setAttribute( "channels", "2" );
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
// Parse the XML.
print $xml->saveXML();
Source: http://www.php.net/manual/en/class.domdocument.php
createElement( name, value ) // just to clarify how this will look in an xml file: <name>value</name>
3. more about writing normal text files
This is the minimum required code, to write some data to a file:
$fp = fopen('data.txt', 'w');
fwrite($fp, '' . $your_text . '');
fclose($fp);
But use something like this, for better security:
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Source: http://www.php.net/manual/en/function.fwrite.php
You can write objects/arrays to a file with JSON
(which serializes when writing and deserializes when reading the data): http://si1.php.net/manual/en/book.json.php
Check these 2 links also: http://si1.php.net/manual/en/function.json-encode.php and http://si1.php.net/manual/en/function.json-decode.php
And then you can read your file with :
$file = file_get_contents('./people.txt');
or to read only a part of the file:
<?php
// Read 14 characters starting from the 21st character
$section = file_get_contents('./people.txt', NULL, NULL, 20, 14);
var_dump($section);
?>
http://www.php.net/manual/en/function.file-get-contents.php