从数据库到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

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.

Try this

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

    }
}

try below code this will work.

you are making json on each while loop.

Which is not complete

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

You can add all elements into the single array. And then encode the array to 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);
    }
<?php    
    $query = "SELECT * FROM webservice";
    $result = mysqli_query($mysqli, $query);
    if($result) {
        $data = mysqli_fetch_all($result, MYSQLI_ASSOC));
        echo json_encode($data);
    }
?>