简体   繁体   English

php从mysql db获取数据并将其存储在xml文件中

[英]php take data from mysql db and store them in xml file

i write a php file to connect in a mysql db and take back the last insert. 我写了一个PHP文件,以连接在MySQL数据库,并取回最后插入。 i want this last insert to store in xml file but i didnt find any solution. 我希望此最后一个插入存储在xml文件中,但我没有找到任何解决方案。 i want the values from mysql to store them in a XML file but i dont know the way.my code is this: 我希望mysql的值将它们存储在XML文件中,但我不知道way.my代码是这样的:

'<?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>"; 

 ?> '
$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. The answer to your question - UPDATE 1.您问题的答案-更新

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 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. More about writing XML files 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();

Source: http://www.php.net/manual/en/class.domdocument.php 资料来源: 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 3.有关编写普通文本文件的更多信息

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 资料来源: 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 您可以使用JSON将对象/数组写入文件(写入时进行序列化,读取数据时进行反序列化): http : //si1.php.net/manual/zh/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 还要检查这两个链接: http : //si1.php.net/manual/en/function.json-encode.phphttp://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 http://www.php.net/manual/en/function.file-get-contents.php

Your question is pretty broad and you might just have it more easy to isolate sub-problems here. 您的问题范围很广,您可能会更容易在这里隔离子问题。 Stackoverflow works best asking a single, concrete question. Stackoverflow最适合问一个具体问题。

So if you divide your problem, you can more easily solve the overall problem by solving the sub-steps. 因此,如果您划分问题,则可以通过解决子步骤来更轻松地解决整体问题。 Not only is that easier then for you, but also the format of Stackoverflow requires that you ask a single, concrete programming question at a time. 这不仅为您带来了便利,而且Stackoverflow的格式要求您一次提出一个具体的编程问题。 Otherwise the answers tend to become bloated and aren't helpful any longer. 否则答案往往会变得肿,不再有用。 Because they become unclear over time, your question is easy to misread etc. . 由于它们随着时间的推移变得不清楚,因此您的问题很容易被误读等。

I hope these two example questions leave you at least the pointers to write the code you need. 我希望这两个示例问题至少能为您提供编写所需代码的指针。

I also highly recommend you to use a more modern database API (preferable PDO ), as it's easier to deal with (eg overall API and the looping is much improved). 我还强烈建议您使用更现代的数据库API(首选PDO ),因为它更易于处理(例如,总体API和循环得到了很大改善)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM