Php JSON Response Array
- by Nick Kl
I have this php code. As you can see i query a mysql database through a function showallevents. I return a the $result to the $event variable. I try to return all rows of the data that i take with the msql_fetch_assoc. I don't get response even when i encode the $response variable. It returns null to all fields. Can anyone help me on what i am doing wrong. I had a valid code but it was returning only 1 row of data so i tried to make an associative array but seems i am failing.
if ($tag == 'showallevents')
{
// Request type is show all events
// show all events
$event = $db->showallevents();
if ($event != false)
{
$data = array();
while($row = mysql_fetch_assoc($event))
{
$data[] = array(
$response["success"] = 1,
$response["uid"] = $event["uid"],
$response["event"]["date"] = $event["date"],
$response["event"]["hours"] = $event["hours"],
$response["event"]["store_name"] = $event["store_name"],
$response["event"]["event_information"] = $event["event_information"],
$response["event"]["event_type"] = $event["event_type"],
$response["event"]["Phone"] = $event["Phone"],
$response["event"]["address"] = $event["address"],
$response["event"]["created_at"] = $event["created_at"],
$response["event"]["updated_at"] = $event["updated_at"]);
}
echo json_encode($data);
}
else
{
// event not found
// echo json with error = 1
$response["error"] = 1;
$response["error_msg"] = "Events not found";
echo json_encode($response);
}
}
else
{
echo "Access Denied";
}
}
?>