This is a very simple example which I usually use to get my job done rapidly. In many cases, we don’t need to setup a CMS like Joomla, Drupal, WordPress or even install a framework like Zend, CodeIgniter, CakePHP just for displaying data of a particular table for Flash reader or AJAX parser. It’s so heavy, waste time and unnecessary.
I’m sure that almost PHP developer know how to write a script that connect to MySQL database, read the result and display it in certain ways. However, it may take around few hours to do simple jobs like that if you don’t touch it for few weeks or few months. You need to search around, try to remember where you stored them and collect every piece of codes in projects.
Yes, I said that because that’s my situations, unfortunately. 🙂
In the PHP example below, I just hard code the database connection settings, access data from a table then response the result to a XML format.
<?php $db_host = "localhost"; $db_name = "wordpress312_new"; $db_username = "root"; $db_password = ""; $dbh = mysql_connect($db_host, $db_username, $db_password) or die("Unable to connect to MySQL"); mysql_query('SET NAMES "utf8"'); mysql_select_db($db_name, $dbh) or die("Could not select $db_name"); $sql = "select * from wp_posts where post_status = 'publish'"; $q = mysql_query($sql); $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; $xml .= "<posts>"; while($r = mysql_fetch_assoc($q)) { $xml .= "<post>"; $xml .= " <id><![CDATA[" . $r["id"] . "]]></id>\n"; $xml .= " <post_title><![CDATA[" . $r["post_title"] . "]]></post_title>\n"; $xml .= " <post_excerpt><![CDATA[xml4cloud/" . $r["post_excerpt"] . ".xml]]></post_excerpt>\n"; $xml .= " <guid><![CDATA[xml4cloud/" . $r["guid"] . ".xml]]></guid>\n"; $xml .= " <post_modified><![CDATA[xml4cloud/" . $r["post_modified"] . ".xml]]></post_modified>\n"; $xml .= "</post>"; } $xml .= "</posts>"; header("Content-type: text/xml"); echo $xml; ?> |
Now, I can do a similar job in 5 minutes instead 1 hour with headache. If you think so, let bookmark it somewhere and save time for a coffee.