Following code easy to use "Mysql /Any Database value convert to XML and json using php"
<?php
/* require the user_id as the parameter */
if(isset($_GET['user_id']) && intval($_GET['user_id'])) {
/* soak in the passed variable or set our own */
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
$user_id = intval($_GET['user_id']); //no default
/* connect to the database */
$link = mysql_connect('localhost','root','') or die('Cannot connect to the DB');
mysql_select_db('xmldb',$link) or die('Cannot select the DB');
/* collect data the posts from the db */
$query = "SELECT post_title, guid FROM pages";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
/* create one master array of the records */
posts = array();
//print_r($posts);
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
/* output display as necessary format */
if($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
}
else {
header('Content-type: text/xml');
echo '<posts>';
foreach($posts as $index => $post) {
if(is_array($post)) {
foreach($post as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
foreach($value as $tag => $val) {
echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
}
}
echo '</',$key,'>';
}
}
}
echo '</posts>';
}
/* disconnect from the database */
@mysql_close($link);
}
// XML Output given url run
//http://localhost/test/xmlcreate/xml.php?user_id=2&num=10
// Json Output given url run
//http://localhost/test/xmlcreate/xml.php?user_id=2&format=json
?>
<?php
/* require the user_id as the parameter */
if(isset($_GET['user_id']) && intval($_GET['user_id'])) {
/* soak in the passed variable or set our own */
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
$user_id = intval($_GET['user_id']); //no default
/* connect to the database */
$link = mysql_connect('localhost','root','') or die('Cannot connect to the DB');
mysql_select_db('xmldb',$link) or die('Cannot select the DB');
/* collect data the posts from the db */
$query = "SELECT post_title, guid FROM pages";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
/* create one master array of the records */
posts = array();
//print_r($posts);
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
/* output display as necessary format */
if($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
}
else {
header('Content-type: text/xml');
echo '<posts>';
foreach($posts as $index => $post) {
if(is_array($post)) {
foreach($post as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
foreach($value as $tag => $val) {
echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
}
}
echo '</',$key,'>';
}
}
}
echo '</posts>';
}
/* disconnect from the database */
@mysql_close($link);
}
// XML Output given url run
//http://localhost/test/xmlcreate/xml.php?user_id=2&num=10
// Json Output given url run
//http://localhost/test/xmlcreate/xml.php?user_id=2&format=json
?>
No comments:
Post a Comment