如何显示使用 json 和 PHP 从数据库获取的特定数据


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

我的代码是

<?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);
?>

我的代码的输出是

{"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(6) "Mittal" [2]= string(2) "25" } [2]= array(3) { [0]= 字符串(6) "米塔尔" [2]= 字符串(2) "25" } [2]= 数组(3) { [0]= 字符串(6) "拉克什" [1]= 字符串(6) "米塔尔" [2]= 字符串(2) "25" } [2]= 数组(3) { [0]= 字符串(6) "Rakesh" [1]= 字符串(6) "米塔尔" [2]= 字符串(2) "25" } [2]= 数组(3) { [0]= 字符串(6) "Rakesh" [1]= 字符串(6) "米塔尔" [2]= 字符串(2) "25" } [2]>= 数组(3) { [0]= 字符串(6) "Rakesh" [1]= 字符串(6) "米塔尔" [2]= 字符串(2) "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]=> 数组(3) { [0]=> 字符串(8) "Sangmesh" [1]=> 字符串(10) "Itgampalli" [2]=> 字符串(2) "29" } [8]=> array(3) { [0]=> 字符串(5) "Siddu" [1]=> 字符串(1) "C" [2]=> 字符串(2) "48" } [9]=> 数组(3) { [0]=> 字符串(4) "Raju" [1]=>字符串(6) "Chidri" [2]=> 字符串(2) "29" } [10]=> 数组(3) { [0]=> 字符串(3) "Ram" [1]=> 字符串(5) "Kumar" [2]=> 字符串(2) "58" } [11]=> 数组(3) { [0]=> 字符串(5) "Roopa" [1]=> 字符串(5) "Patil" [2]=> 字符串(2) "23" } [12]=> 数组 (3) { [0]=> 字符串(8) "Jagdeesh" [1]=> 字符串(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]=>字符串(4) "Gada" [2]=> 字符串(2) "26" } [15]=> 数组(3) { [0]=> 字符串(6) "席拉" [1]=> 字符串(7) "库科特" [2]=> 字符串(2) "26" } [16]=> 数组(3) { [0]=> 字符串(4) "Ajay" [1]=> 字符串(1) "K" [2]=> 字符串(2) "23" } [17]=> 数组(3) { [0]=> 字符串(7) "Prakash" [1]=> 字符串(5) "Nandi" [2]=> string(2) "23" } [18]=> array(3) { [0]=> 字符串(8) "Prasanna" [1]=> 字符串(6) "孟买" [2]=> 字符串(2) "28" } [19]=> 数组(3) { [0]=> 字符串(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" } } }

教师名单

格伦泥潭拉克什·米塔尔纳格拉伊·康迪科帕基肖尔·拉古·迪克西特拉杰什·库尔卡尼阿卡什·古普塔桑梅什·伊特甘帕利西杜·拉朱·奇德里拉姆·库马尔鲁帕·帕蒂尔贾格迪什·曼塔莱萨蒂什·哈格希夫·加达希拉·库科特阿杰·普拉卡什·南迪普拉萨纳孟买维什瓦·塞内尔阿琼·萨加

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

1-首先解码,使用json_decode

   $tmp_数组 = json_decode($data);

2- for 循环

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

代码大脑测试。 : )