【问题标题】:Don't display values by using Json decode()不要使用 Json decode() 显示值
【发布时间】:2020-01-12 07:41:37
【问题描述】:

我将 HTML 表中的一些值存储到 MySQL 数据库。它成功存储在数据库中,但是当我使用Json decode() 获取数据时,Html 表没有显示网页中的任何数据,但没有发生任何错误。

这里是php代码:

 <table class="table table-bordered mb-0">
    <thead>
            <tr>
                <th>Medicine Name</th>
                <th>Morning</th>
                <th>Noon</th>
                <th>Noght</th>
            </tr>
    </thead>
             <tbody>

                   <?php
                require_once 'auth/dbconnection.php';

                  $sql = "SELECT * FROM prescription";
                  if($result = mysqli_query($conn, $sql)){
                     if(mysqli_num_rows($result) > 0){

                         $medRecords = json_decode($row['med_records'],true);
                            if (is_array($medRecords) || is_object($medRecords)) {
                                foreach($medRecords as $key => $object) {

             ?>                 <tr>
                                      <td><?php echo $object->medname ?></td>
                                      <td><?php echo $object->morning ?></td>
                                      <td><?php echo $object->noon ?></td>
                                      <td><?php echo $object->night ?></td>                                 
                                </tr>   
                <?php
                    } }}}
                     ?>
                </tbody>
    </table>

这是我的数据库表

我不知道我哪里出错了。如何改进代码段?

【问题讨论】:

  • 签出$medRecords 在将true 作为json_decode() 函数的第二个参数传递后返回一个数组。如果函数返回null,那么您在数据库中的json编码代码的json格式无效。
  • @unclexo 我已经上传了我的database 表。请通过这个。
  • 好吧,我无法使用您提供的图像浏览这些数据。您能在此处提供med_records 列的原始数据吗?
  • @unclexo 显示如下:[{"medname":"","morning":"2","noon":"3","night":"4"}]

标签: javascript php arrays json


【解决方案1】:

请注意,您缺少 fetch 本身:$result-&gt;fetch_assoc() 您使用 var $row 但来自哪里?

我认为你的代码应该是:

$sql = "SELECT * FROM prescription";
if($result = mysqli_query($conn, $sql)){
    if(mysqli_num_rows($result) > 0){
        while ($row = $result->fetch_assoc()) {        <---- Notice this line
            $medRecords = json_decode($row['med_records'],true);
            if (is_array($medRecords) || is_object($medRecords)) {
                foreach($medRecords as $key => $object) {
                ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多