Php 从数据库到json

Php 从数据库到json,php,json,Php,Json,我试图用数据库中的数据对json进行编码。但是遇到麻烦了。对于数据库中的每个条目,它都会创建一个全新的json数组: {“items”:[{“id”:“1”,“title”:“test”,“description”:“test”,“Links”:[{“rel”:“self”,“href”:“bla”}]}}}{“items”:[{“id”:“2”,“title”:“test”,“description”:“teysh”,“Links”:[{“rel”:“self”,“href”:“bla”}]}

我试图用数据库中的数据对json进行编码。但是遇到麻烦了。对于数据库中的每个条目,它都会创建一个全新的json数组:

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

我不希望它每次创建一个新的数组项。我希望它将所有入口放在一个数组中

我使用以下代码生成:

 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);
            }

        }
    }
}    

请帮助

您需要将json_encode从处理数据库中的行的循环中移出。这应该行得通

$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);
    }
}

编辑:

在这种情况下,您将使用post id访问您的帖子,只有当您对每个类别(标题、描述、链接等)有单独的数组时,才可能使用一维数组,这在您的情况下是不可选的

也尽量不要将变量中的首字母大写。

试试这个

$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 {
        $data['items'] = array();
        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);

    }
}

尝试下面的代码,这将工作

您正在每个while循环上生成json

这是不完整的

$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']))));
      }
      $jsonArray['title'] = $data;
      echo json_encode($jsonArray);
    }
}

可以将所有元素添加到单个数组中。然后将数组编码为json

...    
} 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']))));

        }
  echo json_encode($data);
    }

将在每次运行中替换
$data['items']
中的内容。最终,当您执行
json_encode()
时,它将打印最后一行数据。是的,我注意到,现在已经修复了当我尝试此操作时,它会重复数据库数据两次。首先,它用数组行的编号创建一个数组,然后创建项目array@JustinVeenis没有二维数组你不能做你想做的事你能解释一下为什么没有二维数组我不能做我想做的事吗?
...    
} 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']))));

        }
  echo json_encode($data);
    }
<?php    
    $query = "SELECT * FROM webservice";
    $result = mysqli_query($mysqli, $query);
    if($result) {
        $data = mysqli_fetch_all($result, MYSQLI_ASSOC));
        echo json_encode($data);
    }
?>