当我从PHP函数打印JSON时,它';s仅从SQL打印一行

当我从PHP函数打印JSON时,它';s仅从SQL打印一行,php,json,Php,Json,我创建了一个函数从SQL中提取数据并将其转换为JSON,但当我打印数据时,它只返回一行 Db_functions.php public function getfeeds(){ $stmt = $this->con->prepare("select id,title,status,profilepic,created_at,url from news"); $stmt->execute(); $result = $stmt->get_result(

我创建了一个函数从SQL中提取数据并将其转换为JSON,但当我打印数据时,它只返回一行

Db_functions.php

public function getfeeds(){
    $stmt = $this->con->prepare("select id,title,status,profilepic,created_at,url from news");
    $stmt->execute();
    $result = $stmt->get_result();
    $row = array();
    while ($r = $result->fetch_assoc()) {
       $row =$r;
       $nrow['news'] = array($row);
       $json = str_replace("\\/", "/",json_encode($nrow,JSON_PRETTY_PRINT));
       echo'<pre>';
       return $json;
    }
但是输出应该是这样的,有两行

public function getfeeds(){
    $stmt = $this->con->prepare("select id,title,status,profilepic,created_at,url from news");
    $stmt->execute();
    $result = $stmt->get_result();

    $nrow = array();

    while ($r = $result->fetch_assoc()) {

       $nrow[] = $r;
    }

    return json_encode($nrow);
}

while循环中有一个返回,因此它只返回结果集中的第一行

<?php
session_start();
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$feeds= $db->getfeeds();
// its a string and not an array so print_r wont work
echo $feeds;  
?>
这样叫



您在while循环中有返回,这会给您一个clueGood代码缩进,这一点很明显,正如我整理那段代码时所做的那样。@riggsfolly我试图在循环其give null时返回外部value@RiggsFolly现在它只打印第二行。你是否按照我的答案编码?如果我在任何其他功能中遇到任何问题,我是否可以与你进一步联系。是的,发布一个问题
{
    "news": [
        {
            "id": "1",
            "title": "test",
            "status": "this is content",
            "profilepic": "0",
            "created_at": "2016-09-05 12:11:17",
            "url": "0"
        }
    ]
}
{
    "news": [
        {
            "id": "2",
            "title": "JCECE",
            "status": "JCECE 2016 will be conducted on June 5, 2016 by JCECE Board, which is the exam conducting authority for the engineering entrance examination. JCECE 2016 will be conducted to offer admissions to undergraduate engineering courses at the participating institutes of JCECE 2016 in the state of Jharkhand. As of now, there are a total of 19 colleges (government+private) that will offer over 6000 B.E/B.Tech seats to aspiring candidates in Jharkhand. \r\n\r\nApplication Dates:16 Apr 2016 to 16 May 2016\r\n\r\nAdmit Card Date:11 May 2015\r\n\r\nExam Dates:05 Jun 2016\r\n\r\nResult Date:01 Jul 2015 to 10 Jul 2015      ",
            "profilepic": "http://results.jharkhandeducation.net/JCECEB/JCECEB-Logo.jpg",
            "created_at": "2016-09-16 10:14:55",
            "url": "https://jcece.co.in/"
        }
    ]
}
public function getfeeds(){
    $stmt = $this->con->prepare("select id,title,status,profilepic,created_at,url from news");
    $stmt->execute();
    $result = $stmt->get_result();

    $nrow = array();

    while ($r = $result->fetch_assoc()) {

       $nrow[] = $r;
    }

    return json_encode($nrow);
}
<?php
session_start();
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$feeds= $db->getfeeds();
// its a string and not an array so print_r wont work
echo $feeds;  
?>