我想要做的只是顯示「bpm」值... var_dump顯示值,但我仍然無法顯示「bpm」值到屏幕。用php顯示json數據
PHP文件
<?php
include ('open.php'); //open database connection
$sth = mysql_query("SELECT * FROM heartbeatTB");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows = $r;
}
$decoded_json= json_encode($rows);
print_r($decoded_json);
foreach($decoded_json as $de){
echo $de['id']['bpm']; } //not displaying output
echo "<br><br>";
var_dump($decoded_json);
echo "<br><br>endd";
mysql_close($con);
?>
結果:
success connected!!
{"id":"1","bpm":"121 BPM"} //json string
//result should appear here
string(26) "{"id":"1","bpm":"121 BPM"}" //var_dump output
endd
任何幫助表示讚賞
* *我最後的工作代碼
<?php
include ('open.php');
$sth = mysql_query("SELECT bpm FROM heartbeatTB");
//$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows = $r;
}
$string_json= json_encode($rows);
print_r($string_json); ////////////////
echo "<br><br>";
$result=json_decode($string_json);
var_dump($result->bpm); //////////////
echo "<br><br> the answer :".$result->bpm ;
echo "<br><br>";
var_dump($string_json); ////////////
echo "<br><br>end";
mysql_close($con);
?>
加上我更改數據庫從拉丁到UTF8的排序規則。 thx幫助!
爲什麼json在這裏? – underscore
將'$ rows = $ r;'更改爲'$ rows [] = $ r;'您不需要向數組添加新元素,但是您在循環 – Robert
thx用於提醒時覆蓋數組。 – wayne9003