what i want to do is just display the "bpm" value... the var_dump is showing value but i still cannot display the "bpm" value to screen.
PHP file
<?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);
?>
result :
success connected!!
{"id":"1","bpm":"121 BPM"} //json string
//result should appear here
string(26) "{"id":"1","bpm":"121 BPM"}" //var_dump output
endd
any help is appreciated
**my final working code
<?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);
?>
plus i changed the collation of database from latin to UTF8. thx for the helps!
There is the error in your code. Your aim is to add $rows in loop to the array but you don't do it. Instead you override the array with one row element. It happens here:
while($r = mysql_fetch_assoc($sth)) {
$rows = $r; //error
}
You should change this code to:
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
Also consider using mysqli or pdo engine because mysql_* functions are depracated. When you change the code as I wrote about it should encode all your rows instead of only one(which is last).
The second issue is that you want to loop the encoded json string which is string and not the array. You should loop after $rows
foreach($rows as $row) print_r($rows);
or you can decode string with
$decoded_json= json_encode($rows);
$arr = json_decode($decoded_json);
foreach($arr as $r) print_r($r);
but I don't recommend it. You should see difference between json which is encoded to string and the array.
use this:
$decoded_json= json_encode($rows);
print_r($decoded_json);
foreach($rows as $de){
echo $de['bpm']." : ".$rows['id'];
}
no need to encode the $row
array in json and if your encoding it then you need to decode it first before using it in the foreach to make it as array.
Use
$json = '{"id":"1","bpm":"121 BPM"}';
$decoded_json= json_decode($json);
var_dump($decoded_json->bpm);
to show.
for more about this, pls check http://php.net/manual/en/book.json.php
I can spot several oddities in your code:
while($r = mysql_fetch_assoc($sth)) {
$rows = $r;
}
You retrieve and store several rows, then discard all of them but the last one.
$decoded_json= json_encode($rows);
You encode data into JSON and store it in a variable called $decoded_json
.
// string(26) "{"id":"1","bpm":"121 BPM"}"
foreach($decoded_json as $de){
You flatten your array into a plain string and they you attempt to loop it.
I don't have the faintest idea of what you're trying to accomplish but you should start by actually understanding what your code does. Typing random code until it happens to work is called cargo-cult programming and it's a very inefficient technique.