如何编码多行的json? [英] How to encode json with multiple rows?

查看:114
本文介绍了如何编码多行的json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前,我已经浏览了多个平台上的其他示例和Q& A,但它们似乎都无法解决我的问题.我试图通过json从MySQL返回多行.但是,我一直做不到.下面的代码显示了我的尝试.

Before I begin, I have looked through other examples and Q&A's on multiple platforms but none of them seem to solve my problem. I am trying to return multiple rows from MySQL via a json. However, I have been unable to. The code below shows my attempt.

我通过邮递员得到我的回复.第一个while仅返回数据库中的最后一个条目,而do-while返回所有条目,但未正确编码json,因为json输出syntax error,但html部分显示了所有条目.

I get my responses via Postman. The first while returns only the last entry in the database, and the do-while returns all entries but doesn't encode the json properly, as the json outputs syntax error but the html part shows all entries.

<?php
    $dashboard_content_token = $_REQUEST["dashboard_content_token"];
    $token = "g4";

    require(cc_scripts/connect.php);

    $sql = "SELECT * FROM `dashboard_content`";
    $check = strcmp("$token", "$dashboard_content_token");
    $statement = mysqli_query($con, $sql);
    if (check) {
        $rows = mysqli_fetch_assoc($statement);
        if (!$rows) {
            echo "No results!";
        } else {
              while ($rows = mysqli_fetch_assoc($statement)) {
                $news_id = $rows['news_id'];
                $image_url = $rows['image_url'];
                $news_title = $rows['news_title'];
                $news_description = $rows['news_description'];
                $news_article = $rows['news_article'];

                $result['dashboard content: '][] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 

                echo json_encode($result);
        }
        // do {
                // $news_id = $rows['news_id'];
                // $image_url = $rows['image_url'];
                // $news_title = $rows['news_title'];
                // $news_description = $rows['news_description'];
                // $news_article = $rows['news_article'];

                // $result['dashboard content: '][] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 

                // echo json_encode($result);
        //     } while ($rows = mysqli_fetch_assoc($statement));


        mysqli_free_result($statement);
    }
}
?>

推荐答案

这应该有效.您将要使用do...while语句,否则将跳过第一个结果.

This should work. You'll want to use the do...while statement otherwise the first result is skipped.

<?php
    $dashboard_content_token = $_REQUEST["dashboard_content_token"];
    $token = "g4";

    require(cc_scripts/connect.php);

    $sql = "SELECT * FROM `dashboard_content`";
    $check = strcmp("$token", "$dashboard_content_token");
    $statement = mysqli_query($con, $sql);
    if (check) {
        $rows = mysqli_fetch_assoc($statement);
        if (!$rows) {
            echo "No results!";
        } else {

          do {
             $news_id = $rows['news_id'];
             $image_url = $rows['image_url'];
             $news_title = $rows['news_title'];
             $news_description = $rows['news_description'];
             $news_article = $rows['news_article'];

               $result['dashboard content: '][] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 


          } while ($rows = mysqli_fetch_assoc($statement));

        mysqli_free_result($statement);
        echo json_encode($result);
    }
}
?>

关键是将所有结果放入一个数组,然后只执行一个json_encode().当您多次调用json_encode()时,您的API将返回无效的json.

The key is to put all of you results into an array and then just do one json_encode(). When you call json_encode() multiple times, your API will return invalid json.

这篇关于如何编码多行的json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆