Sunday, July 12, 2009

Create XML file with PHP 5 and MySQL

In this tutorial i would like to explain that how to create XML files in PHP 5 after extracting data from the MySQL database.

In this code there is no hard coded node for the XML files , The data is retrived from the database as an associative and transformed to the XML files.

Requirement

There should be DOM extension available in PHP installation.

Code Sample

Let's discuss how many step is here to write this code, there are three step to write the code

(i) Connection from MySQL databse.
(ii) Retrieve data from the database tables.
(iii) Write retrieved data in XML files.

Connection from MySQL databse

$con=mysql_connect('localhost' , 'testuser' , 'pass123');

if( $con)
{
echo "could not connect host 'localhost' . ";
exit();
}
if (!mysql_select_db('demo'))
{
echo "Cannot connect to database 'demo' . ";
exit();

Above script connect with database 'demo' and now we can fetch the data from this database tables.

Retrieve data from the database tables

$query = "SELECT * FROM tablename ";
$result = mysql_query ($query , $con);

Now we have a data amd need to write it in XML format.

Write retrieved data in XML files

Create new XML document

$document= new DOMDocument( '1.0' );

The first element we create in the XML document is known as the root element. Each XML document must have 1 root element.

Create root node


$root
node = $document->createElement('root');
$rootnode = $document->appendChild($rootnode );

Now after creating the root node we have to add the data from the database


while($row = mysql_fetch_assoc($result))
{
$node= $document->createElementNS( 'Item', 'ezobject:object-relation-list' );
$node = $rootnode->appendChild($node);

foreach ($row as $fieldname => $fieldvalue)
{
$child =
$document->createElement($fieldname);
$child =
$node ->appendChild($child);

$nodevalue = $document->createTextNode($fieldvalue);
$nodevalue = $child->appendChild($nodevalue);
}
}
$xml_doc = $document->saveXML();

echo $xml_doc;


The above tutorial help you to create XML file in PHP 5.

Thursday, July 2, 2009

PHP and AJAX

In this article i would like to explain little bit about AJAX and how it works with PHP.

What is AJAX?

AJAX(Asynchronus JavaScript and XML) is based on JavaScript and HTTP Request.

With AJAX JavaScript can communicate directly with the server, with theXMLHttpRequestobject. With this object, a JavaScript can fetch data with a web server without reloading the page.

AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, allowing web pages to request requried data(information) from the server instead of whole pages.

What is HTTP Request ?

HTTP (Hypertext Transfer Protocol) is a request/response protocol,Whenever web browser fetches a page (date) from a web server, it does so using HTTP. This means your web browser sends a request for some data and the web server sends back a response using the HTTP.

What is XMLHttpRequest ?

XMLHttpRequest is a JavaScript object that was designed by Microsoft,The XMLHttpRequest object provides a way to communicate with a server after a web page has loaded, it's support all the major browaer like Internet Explorer, Firefox, safari.

After explaining the above important terms let see how PHP works with the AJAX
PHP and AJAX :
working with PHP and Ajax is very simple

Step I: we need to know how to create an XMLHTTPRequest object. The process differs slightly depending on whether you are using Internet Explorer (5+) with ActiveX enabled, or a standards-compliant browser such as Mozilla Firefox.

With IE5 and IE6 , the request like:

http = new ActiveXObject("Microsoft.XMLHTTP");

With IE7+, Firefox, Chrome, Opera, Safari.we can instantiate the object directly like:

http = new XMLHttpRequest();

Step 2: We need to write an event handler which will be called via some event on our page, and will handle sending our request for data to our server.
The event handler will use various methods of our XMLHTTPRequest object to:

(i) make the request of the server
(ii) check when the server says that it has completed the request
(iii) deal with the information returned by the server

using above two step you can write PHP application using AJAX.