簡體   English   中英

如何顯示使用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] =>字符串(5)” Glenn“ [1] =>字符串(8)“ Quagmire” [2] =>字符串(2)“ 33”} [1] =>數組(3){[0] =>字符串(6)“ Rakesh” [1] =>字符串(6) “ Mittal” [2] =>字符串(2)“ 25”} [2] =>數組(3){[0] =>字符串(6)“ Nagraj” [1] =>字符串(10)“ Kondikoppa” [2] =>字符串(2)“ 23”} [3] =>數組(3){[0] =>字符串(7)“ Kishore” [1] =>字符串(1)“ K” [2] =>字符串(2)“ 28”} [4] =>數組(3){[0] =>字符串(5)“ Raghu” [1] =>字符串(5)“ Dixit” [2] =>字符串(2)“ 35”} [5] =>數組(3){[0] =>字符串(6)“ Rajesh” [1] =>字符串(8)“ Kulkarni” [2] =>字符串(2) “ 36”} [6] => array(3){[0] =>字符串(6)“ Aakash” [1] =>字符串(5)“ Gupta” [2] =>字符串(2)“ 45” } [7] =>數組(3){[0] =>字符串(8)“ Sangmesh” [1] =>字符串(10)“ Itgampalli” [2] =>字符串(2)“ 29”} [8 ] => array(3){[0] => string(5)“ Siddu” [1] => string(1)“ C” [2] => string(2)“ 48”} [9] => array(3){[0] =>字符串(4)“ Raju” [1] =>字符串(6)“ Chidri” [2] =>字符串(2)“ 29”} [10] =>數組(3 ){[0] =>字符串(3)“ Ram” [1] => s tring(5)“ Kumar” [2] =>字符串(2)“ 58”} [11] =>數組(3){[0] =>字符串(5)“ Roopa” [1] =>字符串(5 )“ Patil” [2] => string(2)“ 23”} [12] => array(3){[0] => string(8)“ Jagdeesh” [1] => string(8)“ manthale “ [2] =>字符串(2)” 26“} [13] =>數組(3){[0] =>字符串(6)” Satish“ [1] =>字符串(6)” kharge“ [2 ] =>字符串(2)“ 29”} [14] =>數組(3){[0] =>字符串(4)“ Shiv” [1] =>字符串(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] =>字符串(4)“ Ajay” [1] =>字符串(1)“ K” [2] =>字符串(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)“孟買” [2] => string(2)“ 28”} [19] = > array(3){[0] =>字符串(6)“ Vishwa” [1] =>字符串(7)“ Saineer” [2] =>字符串(2)“ 23”} [20] => array( 3){[0] =>字符串(5)“ Arjun” [1] =>字符串(5)“ sarja” [2] =>字符串(2)“ 55”}}}

教師名單

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 Kukotteja Ajay K Prakash Nandi Prasash

1-首先解碼,使用json_decode

$tmp_array = json_decode($data);

2-循環

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

經過大腦測試的代碼。 :)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM