通过 PHP 获取 MySQL 数据库输出到 XML [英] Get MySQL database output via PHP to XML

查看:29
本文介绍了通过 PHP 获取 MySQL 数据库输出到 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个 MySQL 数据库,我想知道如何通过 PHP 获取表格中以下列的 XML 输出:

I have a MySQL database on my website, and I would like to know how I could get an XML output via PHP of the following columns in the table:

  1. udid
  2. 国家

推荐答案

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 数据库输出到 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆