1

I have a webservice name geteventsPost.php which is basic code to return data in JSON format

I tried to debug in browser tools, from which it seems like in that variable event there is always one record short. Very first record of the query gets dumped Here is my JSON webservice code I tried to see the response in web browser it look like it returns one record short.

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

So i just want to know that what am i doing wrong? I am guessing its in the script code some where i am handling the response wrong.

Let me know thank you

2
  • and what is the result of the json? Commented Sep 22, 2015 at 0:20
  • @RedAcid i solved it using the answer below.. It was issue with $result i was handling it wrong.. thank you anyway Commented Sep 22, 2015 at 0:22

1 Answer 1

1

Check this out:

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

I don't know more about how you are handling the json but you should add that if this doesn't answer your question.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you.. i got it.. it was the issue with the $result->fetch(), it should be $result->rowCount() > 0 solved the problem...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.