dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using YAML to Decrease Data Transfer Bandwidth Requirements : Page 3

This new alternative to XML ain't markup language, but for data serialization, it serves the same purpose, albeit with greater brevity!


advertisement
Writing the PHP Script
For this article, the goal is to create a PHP script that queries this database and generates YAML from it. The brevity of the following PHP code reflects the ease and brevity of YAML itself.

   <?php
   $dbconn = @mysql_connect('localhost', 'root', '');
   if (!$dbconn)
   {
   die('Error connecting to DB!');
   }
   if (! @mysql_select_db('PriceHistory') ) 
   { 
      die( '<p>Unable to locate the main ' . 
               'database at this time.</p>' ); 
   } 
   $strSql = "select * from Prices";   
   $result= mysql_query($strSql) or die(mysql_error());
   echo("---\r\n");
   if($result)
   {
     while  ($row = mysql_fetch_array($result))
     {
      echo("Day: \"" . $row['date'] . "\"\r\n");
      echo("values:\r\n");
      echo(" - open: " . $row['open'] . "\r\n");
      echo(" - close: " . $row['close'] . "\r\n");
      echo(" - high: " . $row['high'] . "\r\n");
      echo(" - low: " . $row['low'] . "\r\n");
     }
   }
   ?>
The code is quite simple—it outputs a YAML document with the format shown in Table 1.

Table 1. Output YAML Document: The table shows a single record from the YAML document created by querying the database and formatting the results in PHP, along with comments explaining the YAML syntax.
YAML Comments
---

Day: "2004-12-27"

Values:

open: 27.01

close: 26.85

high: 27.10

low: 26.82

Day:

.
Start the document

Start a data section, for this day



We'll be populating values with a series of data

Use '-' for a series of data. Therefore, the values 
for open, close, high and low are in the values



Start another day series for this day



Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date