从json转换的mysql数据检索值时遇到问题

时间:2019-06-24 16:18:33

标签: javascript php mysql json

我已将数据库值转换为json,我想从转换后的值中获取值。

json_data.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdatabase";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT data FROM machinedata ORDER by id DESC LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows >0) {
 // output data of each row
 while($row[] = $result->fetch_assoc()) {

 $tem = $row;

 $json = json_encode($tem);
 $json = preg_replace('/\\\"/',"\"", $json);


 }

} else {
 echo "0 results";
}
 echo $json;
$conn->close();
?>

输出:

[{"data":"{"ID": 2,"HR": 0,"SPO2": 0,"TEMP": 0,"SYS": -1,"DIA": -1,"MEAN": -1,"ECG": ""}"}]

现在,我想获取HR的值为0和SYS的值为-1。 我对此并不陌生,但我没有获得如何获取该价值的方法。

我尝试了json_decode,因为它以json的形式存储在数据库中,但是返回了空白页。

fetch.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdatabase";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT data FROM machinedata ORDER by id DESC LIMIT 1";
$result = $conn->query($sql);

$json = json_decode($result);
echo $json;
$conn->close();
?>

在此先感谢任何解决方案。

0 个答案:

没有答案