需要你的帮助,因为我被困在这里了。我有 MySQL 数据库和 HTML 表单。我可以通过该表单连接到我的数据库,没有任何问题。我的所有按钮(查找、删除、更新和添加)也都可以完美工作。现在我决定添加打印按钮,该按钮应该将检索到的数据导出到 xml 文件(稍后将转换为 pdf 文件,但我知道如何处理该文件) )。知道如何或从哪里开始吗?我可以使用一些 php 函数吗?
实际上,我通过为“打印”按钮创建函数然后稍后调用该函数来解决该问题。一切工作完美。
function printRecords($query){
global $ID, $Name, $Address,$Country, $Ph_number;
$result = mysql_query($query);
$resultCount = mysql_num_rows($result);
$query= 'SELECT ID, Name, Address , Country, Ph_number FROM people';
$fp= @fopen ("somefile.xml","w")or
die ("Could not open file");
$data = "";
for ($r=0; $r<$resultCount; $r++) {
$rec = mysql_fetch_assoc($result);
$data = sprintf("<Name>%s</Name>\r\n<Address>%s</Address>\r\n
<Ph_number>%s</Ph_number>\r\n"
$rec['Name'], $rec['Address'], $rec['Ph_number'] );
fwrite($fp, $data);
}
@fclose($fp);
return($resultCount);
}
最佳答案
尝试过通过 mysql 命令行导出 xml 吗?
例如
mysql -uroot -p --xml -e 'SELECT * FROM mydb.Table1' > table1.xml
请参阅MySQL manual command options for xml
我刚刚看到您可以通过 html 表单访问数据库,但可能无法运行命令行?
否则,您可以从选择中执行您自己的 xmldoc,这是我在此处进行的一个简单查询 SQLFIDDLE .
create table animal(id int, name char(10));
insert into animal values
(1,'dog'),
(2,'cat'),
(3,'mouse'),
(4,'horse'),
(5,'cow');
SELECT
CONCAT('\n<animals>\n',
GROUP_CONCAT('<animal id=', id, '>',name,'</animal>\n' SEPARATOR ''),
'</animals>') AS xmldoc
FROM animal;
输出为:
<animals>
<animal id=1>dog</animal>
<animal id=2>cat</animal>
<animal id=3>mouse</animal>
<animal id=4>horse</animal>
<animal id=5>cow</animal>
</animals>
关于php - 如何从 MySQL 数据库检索数据并将其保存到 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19228144/