sql >> Databasteknik >  >> RDS >> Mysql

Få MySQL-databasutdata via PHP till XML

Ett exempel med 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();

Utdata:

<?xml version="1.0"?>
<countries>
 <country udid="1">Country 1</country>
 <country udid="2">Country 2</country>
 ...
 <country udid="n">Country n</country>
</countries>


  1. SQL Server Bulk Insert – Del 2

  2. Skapar användare med krypterat lösenord i PostgreSQL

  3. Hur man aktiverar allmänna loggar och felloggar i AWS RDS

  4. Synkronisera offline SQLite databas med online MySQL databas