phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


AJAX - MySQL: Creating XML Data use DOM

Tag: AJAX, DOM, XML, XMLHttpRequest, JavaScript, mysql, database   Category: PHP Application
post: 23 Mar 2008 read: 660


AJAX MySQL Step By Step Tutorial - Part 2: Our AJAX Client will read XML data from server. For this jobs, we will create xml document.

Create a file named "server_books.php" within www/test/ajax. Enter following code:


<?
mysql_connect('localhost','root','admin');
mysql_select_db('test');

$sql = "Select * from `books`";
$q   = mysql_query($sql) or die(mysql_error());

while($r = mysql_fetch_array($q)){
  echo $r['id'];
  echo $r['title'];
  echo $r['author'];
  echo "<br />";
}

?>

Test it. Point your browser to http://localhost/test/ajax/server_books.php. You may get like this:

retrieve data from mysql

Now, we create xml document. Modify like this:


<?
header('Content-type: text/xml');

mysql_connect('localhost','root','admin');
mysql_select_db('test');

$sql = "Select * from `books`";
$q   = mysql_query($sql) or die(mysql_error());


$dom = new DOMDocument();

$datas = $dom->createElement('datas');
$dom->appendChild($datas);

while($r = mysql_fetch_array($q)){
  $id = $dom->createElement('id');
  $idText = $dom->createTextNode($r['id']);
  $id->appendChild($idText);

  $title = $dom->createElement('title');
  $titleText = $dom->createTextNode($r['title']);
  $title->appendChild($titleText);

  $author = $dom->createElement('author');
  $authorText = $dom->createTextNode($r['author']);
  $author->appendChild($authorText);

  $book = $dom->createElement('book');
  $book->appendChild($id);
  $book->appendChild($title);
  $book->appendChild($author);

  $datas->appendChild($book);

}


$xmlString = $dom->saveXML();
echo $xmlString;

?>

Next, test it. Point your browser to http://localhost/test/ajax/server_books.php. You may get like this:

retrieve data from mysql and create xml document



Series this article:
AJAX - MySQL: Creating Database
AJAX - MySQL: Creating XML Data use DOM
AJAX - MySQL: Creating Client Side

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting