简体   繁体   中英

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. i want this last insert to store in xml file but i didnt find any solution. i want the values from mysql to store them in a XML file but i dont know the way.my code is this:

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

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

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.

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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