'<?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());
echo "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<th>currentDirection :</th> <td>".$info['currentDirection'] . "</td> ";
echo "<tr>";
echo "<th>light :</th> <td>".$info['light'] . "</td> ";
echo "<tr>";
echo "<th>pressure :</th> <td>".$info['pressure'] . "</td> ";
echo "<tr>";
echo "<th>lhumidity :</th> <td>".$info['humidity'] . "</td> ";
echo "<tr>";
echo "<th>tempC :</th> <td>".$info['tempC'] . "</td> ";
echo "<tr>";
echo "<th>rainin :</th> <td>".$info['rainin'] . "</td> ";
echo "<tr>";
echo "<th>windSpeed :</th> <td>".$info['windSpeed'] . "</td> ";
}
echo "</table>";
?> '
答案 0 :(得分:1)
$xml_file = new DOMDocument();
$windSpeed = $info['windSpeed'];
$xml_windSpeed = $xml->createElement("windSpeed");
$xml_windSpeed->appendChild($windSpeed);
$xml_file->appendChild( $xml_windSpeed );
$xml_file->save("/documents/windSpeed.xml"); //Put there your path
答案 1 :(得分:0)
<强> 1。您的问题的答案 - 更新
使用行:
<?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。您的问题的答案
<?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。有关撰写XML
个文件
// "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();
来源: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。更多关于编写普通文本文件的信息
这是将一些数据写入文件所需的最低代码:
$fp = fopen('data.txt', 'w');
fwrite($fp, '' . $your_text . '');
fclose($fp);
但是为了更好的安全性,请使用类似的东西:
<?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";
}
?>
来源:http://www.php.net/manual/en/function.fwrite.php
您可以将对象/数组写入JSON
的文件(在写入时序列化,在读取数据时反序列化):http://si1.php.net/manual/en/book.json.php
还请检查以下两个链接:http://si1.php.net/manual/en/function.json-encode.php和http://si1.php.net/manual/en/function.json-decode.php
然后你可以用以下内容阅读你的文件:
$file = file_get_contents('./people.txt');
或只读取文件的一部分:
<?php
// Read 14 characters starting from the 21st character
$section = file_get_contents('./people.txt', NULL, NULL, 20, 14);
var_dump($section);
?>
答案 2 :(得分:0)
您的问题非常广泛,您可能更容易在此处隔离子问题。 Stackoverflow最能解决一个具体问题。
因此,如果您将问题分开,则可以通过解决子步骤来更轻松地解决整体问题。这不仅对您来说更容易,而且Stackoverflow的格式要求您一次只询问一个具体的编程问题。否则,答案往往变得臃肿,不再有用。因为它们随着时间的推移变得不清楚,你的问题很容易被误读等。
我希望这两个示例问题至少为您提供编写所需代码的指示。
我还强烈建议您使用更现代的数据库API(最好是PDO),因为它更容易处理(例如整体API和循环得到很大改进)。