Php JSON响应转储数组的第一条记录
我有一个webservice名称Php JSON响应转储数组的第一条记录,php,json,Php,Json,我有一个webservice名称geteventsPost.php 这是以JSON格式返回数据的基本代码 我尝试在浏览器工具中调试,从中可以看出变量event中总是有一条记录短。 查询的第一条记录被转储 这是我的JSONwebservice代码 我尝试在web浏览器中查看响应,它看起来像是短返回一条记录 <?php include 'dbconnection.php'; /* * Following code will list all the products */ // ar
geteventsPost.php
这是以JSON格式返回数据的基本代码
我尝试在浏览器工具中调试,从中可以看出变量event
中总是有一条记录短。
查询的第一条记录被转储
这是我的JSON
webservice代码
我尝试在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);
}
?>
所以我只想知道我做错了什么?
我猜脚本代码中的某些地方我处理的响应是错误的
让我知道
谢谢请查看以下内容:
if ($result->rowCount() > 0) {
$response = array();
while ($row = $result->fetch()) {
$response["event"][] = $row;
}
$response["success"] = 1;
echo json_encode($response);
}
我不知道你是如何处理json的,但如果这不能回答你的问题,你应该补充一点。json的结果是什么?@RedAcid我用下面的答案解决了它。。这是一个问题,
$result
我处理错了。。无论如何谢谢你谢谢你。。我得到了它。。这是$result->fetch()
的问题,应该是$result->rowCount()>0
解决了这个问题。。。