Friday, August 12, 2011

Create Json file using php from mysql database data

<?php
$link = mysql_pconnect("localhost", "root", "siva123") or die("Could not connect");
mysql_select_db("jacksiva") or die("Could not select database");

$arr = array();
$rs = mysql_query("SELECT * FROM ss_emp");

while($obj = mysql_fetch_array($rs)) {
$arr[] = $obj;

}
$json = '{"sample":'.json_encode($arr).'}';
echo $json;
?>

Thursday, August 11, 2011

PHP JSON

Hi friends,
If we want to get data from an url which .json format, here is the code..
$url = "yoururl.json";
$json = @file_get_contents($url);
$statuses = json_decode($json);

Now the all details in that url will be in variable $statuses .

If suppose we to get data from HTML tag which is named text

$status_text = $statuses->text;
If we are having number of text tag and we want 1st tag we have to mention as array
$status_text = $statuses[0]->text;

If we want to get value from element which is inside the element user then

$name = $statuses[0]->user->text;