编码一个完美的json

Han*_*urr 0 php mysql arrays json

我有这个代码

$md_query = "SELECT * FROM table ORDER BY id ASC";
$md_result = mysql_query($md_query, $con);

header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');

while($md_row=mysql_fetch_array($md_result))
    $data_row = array(
        'id' => $md_row['id'],
        'type' => $md_row['type'],
        'title' => $md_row['title'],
        'content' => $md_row['content'],
        'author' => $md_row['postedby'],
        'post_date' => $md_row['posteddate'],
        'publish' => $md_row['publish']
    );

print json_encode($data_row); `
Run Code Online (Sandbox Code Playgroud)

但我只显示1条记录......有人如何解决这个问题?

I a*_*ica 5

你的while循环不包含print语句...所以,它循环遍历所有记录,$data_row每次完全重置,然后在完成后打印一次.

要包含您需要使用的多个语句{并}封装块.