简体   繁体   中英

How to display specific data fetched from database using json with PHP

My code is

<?php /*My PHP/JSON 
              Code */
$con = mysql_connect("localhost","root","root");/*establish connection*/
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$con = mysql_connect("localhost","root","root");

mysql_select_db("TEACHER", $con);
$result = mysql_query("SELECT * from TEACHER") or die('Could not query');
$json = array();
if(mysql_num_rows($result)){
        $row=mysql_fetch_assoc($result);
    while($row=mysql_fetch_row($result)){
        $test_data[]=$row;
    }
    $json['employees']=$test_data;
}
$encoded = json_encode($json);
echo $encoded;
echo "<br />";
echo "<br />";
echo var_dump(json_decode($encoded));
echo "<br />";
echo "<br />";
$tmp_array = json_decode($encoded);

echo "List of Teachers";
echo "<br />";
echo "---------------------------------";
echo "<br />";
foreach($tmp_array->employees as $item)
{
    echo $item[0] . " " . $item[1];
    echo "<br />";
}
mysql_close($con);
?>

the output of my code is

{"employees":[["Glenn","Quagmire","33"],["Rakesh","Mittal","25"],["Nagraj","Kondikoppa","23"],["Kishore","K","28"],["Raghu","Dixit","35"],["Rajesh","Kulkarni","36"],["Aakash","Gupta","45"],["Sangmesh","Itgampalli","29"],["Siddu","C","48"],["Raju","Chidri","29"],["Ram","Kumar","58"],["Roopa","Patil","23"],["Jagdeesh","manthale","26"],["Satish","kharge","29"],["Shiv","Gada","26"],["Sheela","Kukotte","26"],["Ajay","K","23"],["Prakash","Nandi","23"],["Prasanna","Mumbai","28"],["Vishwa","Saineer","23"],["Arjun","sarja","55"]]}

object(stdClass)#1 (1) { ["employees"]=> array(21) { [0]=> array(3) { [0]=> string(5) "Glenn" [1]=> string(8) "Quagmire" [2]=> string(2) "33" } [1]=> array(3) { [0]=> string(6) "Rakesh" [1]=> string(6) "Mittal" [2]=> string(2) "25" } [2]=> array(3) { [0]=> string(6) "Nagraj" [1]=> string(10) "Kondikoppa" [2]=> string(2) "23" } [3]=> array(3) { [0]=> string(7) "Kishore" [1]=> string(1) "K" [2]=> string(2) "28" } [4]=> array(3) { [0]=> string(5) "Raghu" [1]=> string(5) "Dixit" [2]=> string(2) "35" } [5]=> array(3) { [0]=> string(6) "Rajesh" [1]=> string(8) "Kulkarni" [2]=> string(2) "36" } [6]=> array(3) { [0]=> string(6) "Aakash" [1]=> string(5) "Gupta" [2]=> string(2) "45" } [7]=> array(3) { [0]=> string(8) "Sangmesh" [1]=> string(10) "Itgampalli" [2]=> string(2) "29" } [8]=> array(3) { [0]=> string(5) "Siddu" [1]=> string(1) "C" [2]=> string(2) "48" } [9]=> array(3) { [0]=> string(4) "Raju" [1]=> string(6) "Chidri" [2]=> string(2) "29" } [10]=> array(3) { [0]=> string(3) "Ram" [1]=> s tring(5) "Kumar" [2]=> string(2) "58" } [11]=> array(3) { [0]=> string(5) "Roopa" [1]=> string(5) "Patil" [2]=> string(2) "23" } [12]=> array(3) { [0]=> string(8) "Jagdeesh" [1]=> string(8) "manthale" [2]=> string(2) "26" } [13]=> array(3) { [0]=> string(6) "Satish" [1]=> string(6) "kharge" [2]=> string(2) "29" } [14]=> array(3) { [0]=> string(4) "Shiv" [1]=> string(4) "Gada" [2]=> string(2) "26" } [15]=> array(3) { [0]=> string(6) "Sheela" [1]=> string(7) "Kukotte" [2]=> string(2) "26" } [16]=> array(3) { [0]=> string(4) "Ajay" [1]=> string(1) "K" [2]=> string(2) "23" } [17]=> array(3) { [0]=> string(7) "Prakash" [1]=> string(5) "Nandi" [2]=> string(2) "23" } [18]=> array(3) { [0]=> string(8) "Prasanna" [1]=> string(6) "Mumbai" [2]=> string(2) "28" } [19]=> array(3) { [0]=> string(6) "Vishwa" [1]=> string(7) "Saineer" [2]=> string(2) "23" } [20]=> array(3) { [0]=> string(5) "Arjun" [1]=> string(5) "sarja" [2]=> string(2) "55" } } }

List of Teachers

Glenn Quagmire Rakesh Mittal Nagraj Kondikoppa Kishore K Raghu Dixit Rajesh Kulkarni Aakash Gupta Sangmesh Itgampalli Siddu C Raju Chidri Ram Kumar Roopa Patil Jagdeesh manthale Satish kharge Shiv Gada Sheela Kukotte Ajay K Prakash Nandi Prasanna Mumbai Vishwa Saineer Arjun sarja

1- decode first, use json_decode

$tmp_array = json_decode($data);

2- for loop

foreach($tmp_array->employees as $item)
    {
        echo $item[0] . "/" . $item[1] . "/" . $item[2];
    }

code brain tested. : )

$decoded = json_decode($json);
foreach ($decoded->employees as $emp)
  printf("%s %s\n", $emp[0], $emp[1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM