gpt4 book ai didi

php - Json 不会在 php 中响应我的数组

转载 作者:行者123 更新时间:2023-11-29 22:21:12 25 4
gpt4 key购买 nike

我正在尝试从数据库中获取多行,然后使用 json 对它们进行编码,以便我可以在 Android 应用程序中访问它们。我已经成功地编码了一个对象,但我找不到对数组执行相同操作的方法。我的代码如下所示:

if ($tag == ' friend ') {

    $id = $_POST['id'];

$friends = $db->getMyFriends($id);
if ($friends != false) {
// friends found
$result[] = array();
while($row = mysql_fetch_assoc($friends)){
$response[ "error"] = FALSE;
$result[] = array(
$response["friends"]["unique_id"] = $row["unique_id"],
$response["friends"]["name"] = $row["name"],
$response["friends"]["email"] = $row["email"]);
}

echo json_encode($response);
}

来自 getMyFriends($id) 的代码我已经测试过并且工作正常。它返回:

$result = mysql_fetch_array($result);
返回$结果;

当使用rest客户端传递参数时:标签: friend id: $id

这是我得到的 json 响应:{ “标签”:“我的 friend ”, “错误”:错误},但没有实际的数据库数据。

如果有人知道我做错了什么,我真的很感激,我已经浏览互联网几个小时了。

最佳答案

如果 getMyFriends 已有 $result = mysql_fetch_array($result);,则无需再次获取。

您可以简单地:

$friends = $db->getMyFriends($id);
echo json_encode($friends);

但这只会返回一个 friend ,如果您希望从 getMyFriends 中全部删除 $result = mysql_fetch_array($result); 并返回纯 mysql_result,则执行以下操作:

 $result = array();
while($row = mysql_fetch_assoc($friends)){
array_push($result,$row);
}
echo json_encode($result);

关于php - Json 不会在 php 中响应我的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com