<?php
header('Content-type: text/xml');
$db_host="localhost";
$db_user="root"; //用户名
$db_password="123456"; //密码
$db_name="gis"; //数据库名
$link=mysql_connect($db_host,$db_user,$db_password) or die("连接数据库失败");
mysql_query("SET NAMES 'gb2312'",$link); //选择编码,解决mysql的乱码问题
$db=mysql_select_db($db_name,$link);
$table="info";//表名
$fic = fopen("hotspot.xml", "w");//输出的xml名称,目录需要有写的权限
fwrite($fic, '<?xml version="1.0" encoding="GB2312"?>');
fwrite($fic, "<$table>");
$elements =array();
$result = mysql_query("describe $table");
while ($row = mysql_fetch_array($result))
$elements[] = $row[0];
$result = mysql_query("select * from $table");
$i = 0;
while ($row = mysql_fetch_array($result)){
$i++;
fwrite($fic, "<row num=/"$i/">");
for ($j = 0; $j<count($elements) ; $j++){
fwrite($fic, "<$elements[$j]>".htmlspecialchars($row[$j])."</$elements[$j]>");
}
fwrite($fic, "</row>");
}
fwrite($fic, "</$table>");
mysql_close();
fclose($fic);
?>
本文介绍了一个使用PHP从MySQL数据库中读取数据并将其转换为XML文件的方法。此过程包括数据库连接、选择数据、设置字符编码及最终将数据写入XML文件。
161

被折叠的 条评论
为什么被折叠?



