2015-09-21 116 views
1

我有一個web服務的名稱geteventsPost.php 這是基本的代碼以JSON格式返回數據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); 
} 
?> 

所以我只是想知道我做錯了什麼? 我猜測它在腳本代碼中的一些地方,我正在處理錯誤的響應。

讓我知道 謝謝

+0

和json的結果是什麼? –

+0

@RedAcid @RedAcid我使用下面的答案解決了它。它是'$ result'的問題,我正在處理它錯誤..謝謝無論如何 –

回答

1

檢查了這一點:

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

我不知道更多關於你是如何處理的JSON,但你應該補充一點,如果這不回答你的題。

+0

謝謝..我明白了......這是'$ result- > fetch()',它應該是'$ result-> rowCount()> 0'解決了這個問題... –