doufu5401 2015-01-15 11:34
浏览 50
已采纳

从数据库到json

i am trying to encode json with data from my database. But run into trouble. for every entry in my database it creates a compleetly new json array:

{"items":[{"id":"1","title":"test","description":"test","Links":[{"rel":"self","href":"bla"}]}]}{"items":[{"id":"2","title":"test","description":"teysh","Links":[{"rel":"self","href":"bla"}]}]}

i don't want it to create every time a new array items. I want it to put all the entrys in one array.

I generate this using this code:

 function getData($mysqli)
{
    header("HTTP/1.1 302 found", true, 302);

    $query = "SELECT * FROM webservice";
    if ($result = mysqli_query($mysqli, $query)) {
        if (mysqli_num_rows($result) == 0) {
            echo "there are no post, please create one";
        } else {
            while ($post = mysqli_fetch_assoc($result)) {
                $data['items'] = array(array("id" => $post['id'], "title" => $post['title'], "description" => $post['artist'], "Links" => array(array("rel" => "self", "href" => $post['link']))));
                echo json_encode($data);
            }

        }
    }
}    

please help

  • 写回答

5条回答 默认 最新

  • doulian4467 2015-01-15 11:37
    关注

    You need to move json_encode out of the loop that is processing rows from database. This should work.

    $query = "SELECT * FROM webservice";
    if ($result = mysqli_query($mysqli, $query)) {
        if (mysqli_num_rows($result) == 0) {
            echo "there are no post, please create one";
        } else {
            while ($post = mysqli_fetch_assoc($result)) {
                $data[] = array(array("id" => $post['id'], "title" => $post['title'],    "description" => $post['artist'], "Links" => array(array("rel" => "self", "href" => $post['link']))));
            }
            $data['items']=$data;
            echo json_encode($data);
        }
    }
    

    edit:

    $query = "SELECT * FROM webservice";
    if ($result = mysqli_query($mysqli, $query)) {
        if (mysqli_num_rows($result) == 0) {
            echo "there are no post, please create one";
        } else {
            while ($post = mysqli_fetch_assoc($result)) {
                $data[$post['id']]["title"]         = $post['title'];                
                $data[$post["id"]]["description"]   = $post['artist'];
                $data[$post["id"]]["links"]["rel"]  = "self";
                $data[$post["id"]]["links"]["href"] = $post['link'];
            }
            echo json_encode($data);
        }
    }
    

    In this case you will be acessing your post with post-id, one-dimensional array would be only possible if you've had separate arrays for each category(title, description, links, etc) which isn't optional in your case.

    Also try not to capitalize first letters in your variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教