In this tutorial, the examples refers to this database tables:
mysql> select * from bands; +----+--------------+------------+-------------+-------------+ | id | name | birth_year | birth_place | genre | +----+--------------+------------+-------------+-------------+ | 1 | The Blabbers | 1998 | London | Rock'n'Roll | | 2 | Only Stupids | 1997 | New York | Hip Hop | +----+--------------+------------+-------------+-------------+ mysql> select * from albums; +----+---------+------------------+------+-----------------+ | id | bandsID | title | year | comment | +----+---------+------------------+------+-----------------+ | 1 | 1 | BlaBla | 1998 | Their first one | | 2 | 1 | More Talks | 2000 | The second one | | 3 | 2 | All your base... | 1999 | The Classic | +----+---------+------------------+------+-----------------+
The typical using
Let's start with an example using the default options. The new instance is bind to an DSN, so you have only to provide an SQL query. The instance fetches the result automatically; in $xmlstring you found the XML representation of the result set.
If your query result base on joined tables, a nested XML data structure can represent how the DBMS joins the tables. To enable or to disable this behavoir use setOptions() with the option key 'nested'. The default value is TRUE - the nesting is enabled.
<?php $sql2xmlclass = new xml_sql2xml("mysql://username:password@localhost/xmltest"); $xmlstring = $sql2xmlclass->getxml("select * from bands left join albums on bands.id = bandsID"); ?>
<?php $sql2xmlclass = new xml_sql2xml("mysql://username:password@localhost/xmltest"); $options = array('nested' => false); $sql2xmlclass->setOptions($options); $xmlstring = $sql2xmlclass->getxml("select * from bands left join albums on bands.id = bandsID"); ?>
Deliver First Class Web Sites: 101 Essential Checklists Want to learn how to make your web sites usable and accessible? Want to ensure that your sites meet current best practice, without spending hours trawling through incomprehensible specifications and recommendations from dozens of different books, research papers, and web sites? Want to make sure that the sites you build are "right the first time," requiring no costly redevelopments?