Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Connect To MySQL Read Data And Export XML Format

PHP Connect To MySQL Read Data And Export XML Format

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;
?>

<?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.

Aug 26, 2011Hoan Huynh
PHP Show Limited Words Of A String Content Based On Max LengthPHP Save String Content To File
You Might Also Like:
  • How To Track Website With Multiple Google Analytisc Accounts
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • Where MySQL Data Dir And Innodb Data Home Dir Location
  • JavaScript Get Radio Button Value
  • Connect Remotely To MySQL or SQL Server Databases for Godaddy Hosting Account
  • ASP.NET C# Export Data To CSV And Prompt Download
  • MSSQL Export A Table Data To CSV
  • innerText May Not Work On Mozilla Firefox
  • PHP Connect To MS SQL Server
  • Get Image Width Height With JQuery And JavaScript
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

11 years ago PHPheader, mysql_connect, mysql_fetch_assoc, mysql_query, xml273
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,542 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,885 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,736 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,059 views
C# Read Json From URL And Parse/Deserialize Json
11,789 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development