Menu principale:
CREATE TABLE user ( id INT UNSIGNED NOT NULL AUTO_INCREMENT , firstName VARCHAR(100) NOT NULL , lastName VARCHAR(100) NOT NULL , email VARCHAR(100) NOT NULL , PRIMARY KEY (`id`) ) ENGINE = InnoDB; INSERT INTO user (id, firstName, lastName, email) VALUES (NULL, 'Simone', 'D\'Amico', 'simone@email.it'), (NULL, 'Mario', 'Rossi', 'mario@email.it');
$user = 'root'; $password = 'root'; $db = 'test'; $host = 'localhost'; $port = 8889; $conn = new PDO("mysql:host=$host; dbname=$db; port=$port", $user, $password);
$xml = new SimpleXMLElement(''); $stmt = $conn->query("SELECT * FROM user"); while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ $user = $xml->addChild('user'); foreach ($row as $key => $value) { $user->addChild($key, $value); } } echo $xml->asXML();
<?xml version="1.0"?> <users> <user> <id>1</id> <firstName>Simone</firstName> <lastName>D'Amico</lastName> <email>simone@email.it</email> </user> <user> <id>2</id> <firstName>Mario</firstName> <lastName>Rossi</lastName> <email>mario@email.it</email> </user> </users>