JSON响应转储数组的第一条记录

时间:2015-09-21 23:56:06

标签: php json

我有一个网络服务名称geteventsPost.php 这是以JSON格式返回数据的基本代码

我尝试在浏览器工具中进行调试,在该变量event中似乎总是有一个记录短。 查询的第一条记录被转储 这是我的JSON网络服务代码 我试图在Web浏览器中看到响应,它看起来像是返回一条记录。

<?php
include 'dbconnection.php';


/*
 * Following code will list all the products
 */

// array for JSON response
$response = array();

if(isset ($_GET['id'])) {
// get all products from products table
try {
    $sql = "SELECT * FROM event where organiser_id =".$_GET['id'];
    $result = $pdo->query($sql);
}
catch (PDOException $e)
{
    echo 'Error fetching data: ' . $e->getMessage();
    exit();
}

}


// check for empty result
if ($result->fetch() > 0) {
// looping through all results
// master menu node
$response["event"] = array();

while ($row = $result->fetch()) {
    // temp user array
    $event= array();
    $event["id"] = $row["id"];
    $event["title"] = $row["title"];
    $event["location"] = $row["location"];
    $event["start_date"] = $row["start_date"];


    // push single menu into final response array
    array_push($response["event"], $event);
}
// success
$response["success"] = 1;

// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No event found";

// echo no users JSON
echo json_encode($response);
}
?>

所以我只想知道我做错了什么? 我在脚本代码中猜测它处理错误的响应。

让我知道 谢谢

1 个答案:

答案 0 :(得分:1)

检查出来:

if ($result->rowCount() > 0) {
$response = array();
    while ($row = $result->fetch()) {
        $response["event"][] = $row; 
    }
$response["success"] = 1;
echo json_encode($response);
}

我不太了解你如何处理json,但如果你没有回答你的问题,你应该补充一点。