Last updated: August 26, 2026.
Read schema metadata from INFORMATION_SCHEMA with a restricted database account, then write a well-formed XML document with XMLWriter.
Query table and column metadata
<?php
declare(strict_types=1);
$schema = 'application_db';
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=information_schema;charset=utf8mb4',
getenv('DB_USER'),
getenv('DB_PASSWORD'),
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$statement = $pdo->prepare(
'SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_KEY
FROM COLUMNS
WHERE TABLE_SCHEMA = :schema
ORDER BY TABLE_NAME, ORDINAL_POSITION'
);
$statement->execute(['schema' => $schema]);
$columns = $statement->fetchAll(PDO::FETCH_ASSOC);Generate the XML
<?php
$xml = new XMLWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('schema');
$xml->writeAttribute('name', $schema);
foreach ($columns as $column) {
$xml->startElement('column');
foreach ($column as $name => $value) {
$xml->writeElement(strtolower($name), (string) $value);
}
$xml->endElement();
}
$xml->endElement();
$xml->endDocument();
header('Content-Type: application/xml; charset=UTF-8');
echo $xml->outputMemory();Keep database credentials outside the document root and do not expose schema exports publicly. Add indexes, constraints, and relationships from their corresponding INFORMATION_SCHEMA tables when the consumer needs them.
Building an application from an existing database? PHPRunner can generate database-driven web applications without hand-writing the data layer.