【问题标题】:php take data from mysql db and store them in xml filephp 从 mysql db 获取数据并将它们存储在 xml 文件中
【发布时间】:2014-07-09 08:32:43
【问题描述】:

我写了一个 php 文件来连接一个 mysql 数据库并取回最后一个插入。我希望最后一个插入存储在 xml 文件中,但我没有找到任何解决方案。我希望 mysql 中的值将它们存储在 XML 文件中,但我不知道方式。我的代码是这样的:

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

 ?> '

【问题讨论】:

  • 发布您的数据库凭据并不是最好的做法...
  • 你的问题是什么?你的问题是什么?你有任何错误吗?预期的结果是什么,你实际得到了什么?顺便说一句,这是 HTML,而不是 XML(尽管纯粹主义者可能会争论 :-))
  • 我想将此数据存储在 xml 文件中,但我不知道怎么做
  • @user3819412 我已经更新了我的答案。它现在应该可以工作了。检查。

标签: php mysql xml


【解决方案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

【讨论】:

  • 感谢您的回复。我接受了这个错误 可捕获的致命错误:传递给 DOMNode::appendChild() 的参数 1 必须是 DOMNode 的一个实例,在第 28 行的 C:\xampp\htdocs\connect_db_take_data.php 中给出了 null
  • @user3819412 首先,您必须创建一个元素以便稍后附加它。
【解决方案2】:

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.phphttp://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);
?>

http://www.php.net/manual/en/function.file-get-contents.php

【讨论】:

    【解决方案3】:

    您的问题非常广泛,您可能更容易在此处隔离子问题。 Stackoverflow 最适合提出一个具体的问题。

    因此,如果您将问题分解,则可以通过解决子步骤来更轻松地解决整体问题。这不仅对您来说更容易,而且 Stackoverflow 的格式要求您一次提出一个具体的编程问题。否则答案往往会变得臃肿并且不再有用。因为随着时间的推移它们变得不清楚,所以您的问题很容易被误读等。

    我希望这两个示例问题至少能为您提供编写所需代码的指针。

    我还强烈建议您使用更现代的数据库 API(最好是 PDO),因为它更容易处理(例如,整体 API 和循环得到很大改进)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多