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

时间:2012-08-16 06:48:18

标签: php json

我的代码是

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

教师名单

格伦·夸格尔 拉克什米塔尔 Nagraj Kondikoppa 基肖尔K. Raghu Dixit Rajesh Kulkarni 阿卡什古普塔 Sangmesh Itgampalli 西杜都 Raju Chidri 拉姆库马尔 Roopa Patil Jagdeesh manthale Satish kharge 希夫加达 Sheela Kukotte Ajay K. 普拉卡什南迪 Prasanna Mumbai Vishwa Saineer Arjun sarja

2 个答案:

答案 0 :(得分:0)

首先解码,使用json_decode

    $tmp_array = json_decode($data);

2- for loop

    foreach($tmp_array->employees as $item)
    {
        echo $item[0] . "/" . $item[1] . "/" . $item[2];
    }
代码脑测试。 :)

答案 1 :(得分:0)

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