reading data from MYSQL with Xtirer
Hi everyone!
This is the second introductory post to Xtirer 0.2 Beta. After you have installed Xtirer, now let’s move on to the first steps of using it.
Let’s read some data from a database and return the resulting XML.
But first some notes on how Xtirer is working and why:
Xtirer was born due to the need of having various XML output from a database to be read by Flash or Openlaszlo.
Usually the XML you need looks like this:
<rootnode>
<parentnode attribute1="abcd" attribute2="1234">
<childnode attribute="14.5.98">Some longer textnode </childnode>
</parentnode>
<parentnode attribute1="efgh" attribute2="5678">
<childnode attribute="178.3812">Some other longer textnode </childnode>
</parentnode>
...
</rootnode>
So the idea was why not have a Prototype file describing one XML object and indicating where in the database to get the data from and replicating this object as many times as there are entries in the database. So that’s the basic idea behind Xtirer.
Ok, let’s have a basic mysql table called “animals” containing the following columns (taken from mysql.org tutorial):
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
And we want to have a list of all animals indicating name, owner, etc. as a XML-Output. The wrapping xml-object is called “animallist”. So the XML-Prototype file should then look like this:
<animallist>
<xml_object name="pat" source="TABLE.animals">
<xml_object name="name" valueof="COLUMN.name"/>
<xml_object name="owner" valueof="COLUMN.owner"/>
<xml_attribute name="species" valueof="COLUMN.species"/>
<xml_attribute name="sex" valueof="COLUMN.sex"/>
<xml_attribute name="birth" valueof="COLUMN.birth"/>
<xml_attribute name="death" valueof="COLUMN.death"/>
</xml_object>
</animallist>
Save the file into your prototypes directory and if you now call xtirer.php?menuname=animals you should get the following output (click right and select show source or something equivalent):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<animallist>
<pat species="dog" sex="0" birth="2008-07-11" death="2016-07-22">
<name><![CDATA[chaco]]></name>
<owner><![CDATA[mip]]></owner>
</pat>
<pat species="cat" sex="1" birth="2007-07-20" death="2016-07-07">
<name><![CDATA[lurch]]></name>
<owner><![CDATA[ronzo]]></owner>
</pat>
<pat species="ape" sex="0" birth="1997-07-04" death="2007-07-17">
<name><![CDATA[chimpike]]></name>
<owner><![CDATA[lornand]]></owner>
</pat>
</animallist>
I hope it’s somehow visible what has happened:
Xtirer takes the prototype file and scans it for xml-objects / tags it knows. In this case they are the xml_object and xml_attribute tags. The xml_object defines a tag and xml_attribute as the name already says, defines an attribute. In this example i’ve only used three attributes controlling the behaviour of the elements – source, valueof and name.
@ source: If an xml_object or xml_attribute contains such, a new query is build referencing the TABLE indicates – here TABLE = animals
@ valueof: defines the table COLUMN to insert into this object / attribute
@ name: the name / tagname of the xml-object or attributename.
Ok, i stop here for now, but there is much more to come. So check back and have fun with Xtirer.
on July 15th, 2008 at 19:57
[...] my latest development projects. Check out the posts about reading data from MYSQL via Xtirer: Intro, part 2 & part 3 and the guide how to write data to the [...]
on July 15th, 2008 at 20:27
[...] Simple Reading from a MYSQL Database and how the XML-Output is generated [...]