回答
以XMLWriter爲例。
mysql_connect('server', 'user', 'pass');
mysql_select_db('database');
$sql = "SELECT udid, country FROM table ORDER BY udid";
$res = mysql_query($sql);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('countries');
while ($row = mysql_fetch_assoc($res)) {
$xml->startElement("country");
$xml->writeAttribute('udid', $row['udid']);
$xml->writeRaw($row['country']);
$xml->endElement();
}
$xml->endElement();
header('Content-type: text/xml');
$xml->flush();
輸出:
<?xml version="1.0"?>
<countries>
<country udid="1">Country 1</country>
<country udid="2">Country 2</country>
...
<country udid="n">Country n</country>
</countries>
<?php
mysql_connect('myserver', 'username', 'password');
mysql_select_db('mydatabase');
$result = mysql_query('SELECT `udid`, `country` FROM `MyTable`');
while($data = mysql_fetch_assoc($result)) {
foreach($data as $key => $value) {
echo "<$key>$value</$key>";
}
}
?>
這段代碼應該給你一個良好的開端。但是沒有想要的XML結構,很難做得更好。
但是我不確定PHP是否適合這項任務。許多工具,例如phpmyadmin,都可以輸出XML格式的mysql數據。
感謝,我得到的錯誤解析錯誤:語法錯誤,意外「{儘管! – 2011-02-25 00:48:58
在while()中缺少結束')'。另外,在'mysql_queery()'中輸入錯誤的代碼@ – drudge 2011-02-25 00:54:09
<?php
mysql_connect('myserver', 'username', 'password');
mysql_select_db('mydatabase');
$result = mysql_query('SELECT `udid`, `country` FROM `MyTable`');
$Result = "<?xml version='1.0' encoding='utf-8'?>\n<employees>\n";
while($data = mysql_fetch_assoc($Recordset1)) {
$Result .= " <employee>\n";
foreach($data as $key => $value) {
$Result .= " <$key>$value</$key>\n";
}
$Result .= " </employee>\n";
}
$Result .= "</employees>\n";
echo $Result;
?>
我奮鬥了很多,找出在mysqli的格式,這個解決方案,但無處我找到了解決辦法。以下是我想到的解決方案。希望它能幫助一些人。
<?php
//Create file name to save
$filename = "export_xml_".date("Y-m-d_H-i",time()).".xml";
$mysql = new Mysqli('server', 'user', 'pass', 'database');
if ($mysql->connect_errno) {
throw new Exception(sprintf("Mysqli: (%d): %s", $mysql->connect_errno, $mysql->connect_error));
}
//Extract data to export to XML
$sqlQuery = 'SELECT * FROM t1';
if (!$result = $mysql->query($sqlQuery)) {
throw new Exception(sprintf('Mysqli: (%d): %s', $mysql->errno, $mysql->error));
}
//Create new document
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
//add table in document
$table = $dom->appendChild($dom->createElement('table'));
//add row in document
foreach($result as $row) {
$data = $dom->createElement('row');
$table->appendChild($data);
//add column in document
foreach($row as $name => $value) {
$col = $dom->createElement('column', $value);
$data->appendChild($col);
$colattribute = $dom->createAttribute('name');
// Value for the created attribute
$colattribute->value = $name;
$col->appendChild($colattribute);
}
}
/*
** insert more nodes
*/
$dom->formatOutput = true; // set the formatOutput attribute of domDocument to true
// save XML as string or file
$test1 = $dom->saveXML(); // put string in test1
$dom->save($filename); // save as file
$dom->save('xml/'.$filename);
?>
<?php
// Create connection
$con=mysqli_connect("localhost","root","root","students");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//get student ID from URL
$STU_ID = $_GET['id'];
$sql = "SELECT * FROM students_info WHERE ID = ".$STU_ID;
$res = mysqli_query($con, $sql);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('students');
while ($row = mysqli_fetch_assoc($res)) {
$xml->startElement("student");
$xml->writeElement("id", $row['ID']);
$xml->writeElement("name", $row['name']);
$xml->writeElement("gpa", $row['GPA']);
$xml->writeRaw($row['student']);
$xml->endElement();
}
$xml->endElement();
header('Content-type: text/xml');
$xml->flush();
// Free result set
mysqli_free_result($result);
// Close connections
mysqli_close($con);
?>
Output
<students>
<student>
<id>111</id>
<name>sara</name>
<gpa>4.5</gpa>
</student>
</students>
我改進了你的代碼格式。我建議你進一步改進你的答案,給出你的代碼的解釋,包括它的作用以及爲什麼以及如何回答這些問題。 – mmgross 2017-11-19 14:04:55
- 1. 出口MySQL數據庫通過PHP
- 2. 通過PHP從MySQL獲取FLOT數據?
- 3. 無法通過cURL獲取XML輸出
- 4. PHP從mysql數據庫輸出數據到一個XML文件
- 5. 無法通過PHP從XML數據庫獲取記錄
- 6. Php:通過mysql從數據庫中獲取結果集
- 7. 通過PHP PDO從Oracle SP獲取數據[輸出是遊標]
- 8. PHP XML到mysql數據庫
- 9. 無法通過javascript函數從數據庫獲取php數據
- 10. 通過PHP在MySQL數據庫請求後更新XML文件
- 11. CSS href數據庫mysql通過PHP
- 12. 通過php遠程mysql數據庫
- 13. PHP輸出xml數據
- 14. 通過PHPMyAdmin導出MySQL數據庫
- 15. 通過PHP連接到mysql數據庫通過PHP
- 16. 通過https獲取XML數據://
- 17. 通過jquery.ajax獲取XML數據
- 18. Joomla VirtueMart - 通過PHP從數據庫獲取數據
- 19. 通過AJAX獲取從PHP數據庫數據,
- 20. 如何通過HTML表單獲取通過標籤獲得的XML數據庫?
- 21. 使用SimpleXML通過PHP從MySQL數據庫中導出XML的困難
- 22. 在Php MySQL數據庫中取出行
- 23. 通過數據庫的XML
- 24. 表單輸入不通過PHP到mySQL數據庫
- 25. 通過PHP和SQL輸出XML
- 26. 如何從數據庫表中獲取xml輸出
- 27. PHP-MySQL從存儲過程中獲取輸出參數的值
- 28. 從mysql表中反序列化數據並通過php輸出?
- 29. 從MySQL/php獲取數據時出錯
- 30. 如何通過mysql獲取數據
@XcodeDev,你必須修改SQL代碼到你的數據庫模式。 – Czechnology 2011-02-25 00:55:20