-1

我试图检索多行 JSON 数据并显示它,但我无法检索所有行,我只能获取数据库表的顶部(第一)行。我对如何将检索到的 JSON 数据放入一些中感到困惑排序数组并访问各个行?

我已经提供了与问题相关的代码。是的,在发布这个问题之前我已经做了足够的研究。我实际上发现了一个与我的问题相似但没有人回答的问题,所以我正在发布这篇文章。

谢谢你

下面是我的 Freebies.java 类的代码,我试图通过从 UserFunctions.java 类调用 getAllFreebies 函数来检索 JSONdata

UserFunctions uf = new UserFunctions();
JSONObject json = uf.getAllFreebies();
System.out.println(json);

下面是 UserFunctions.java 类中函数 getAllFreebies() 的代码

public JSONObject getAllFreebies(){
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", getAllFreebies_tag));
    JSONObject json = jsonParser.getJSONFromUrl(getAllFreebiesURL,params);
    return json;
}

下面是 index.php 中的代码,其中我从 DB_Function.php 文件调用 getFreebies() 函数

else if($tag = 'getAllFreebies'){
                    $getAllFreebies = $db->getFreebies($username,$catagory,$subcatagory,$title,$condition,$description,$address,$city,$state,$country,$zipcode,$posted_on);
                    if($getAllFreebies)
                    {
            $response["success"] = 1;
            $response["getAllFreebies"]["username"] = $getAllFreebies["username"];
            $response["getAllFreebies"]["catagory"] = $getAllFreebies["catagory"];
            $response["getAllFreebies"]["subcatagory"] = $getAllFreebies["subcatagory"];
            $response["getAllFreebies"]["title"] = $getAllFreebies["title"];
            $response["getAllFreebies"]["item_condition"] = $getAllFreebies["item_condition"];
            $response["getAllFreebies"]["description"] = $getAllFreebies["description"];
            $response["getAllFreebies"]["address"] = $getAllFreebies["address"];
            $response["getAllFreebies"]["city"] = $getAllFreebies["city"];
            $response["getAllFreebies"]["state"] = $getAllFreebies["state"];
            $response["getAllFreebies"]["country"] = $getAllFreebies["country"];
            $response["getAllFreebies"]["zipcode"] = $getAllFreebies["zipcode"];
            $response["getAllFreebies"]["posted_on"] = $getAllFreebies["posted_on"];
            echo json_encode($response);
                        }else {
                            $response["error"] =1;
            $response["error_msg"] = "Error in getAllFreebies";
            echo json_encode($response);

                            }
                    }// end of getAllFreebies tag

下面是我的 DB_function.php 的 getFreebies() 函数的代码,它负责对 MySQL 数据库执行查询。

public function getFreebies(){
$result = mysql_query("SELECT * FROM freebie") or die(mysql_error());
return mysql_fetch_array($result);
}

下面是logcat:

05-30 00:13:23.960: E/JSON(318): {"tag":"getAllFreebies","success":1,"error":0,"getAllFreebies":{"username":"viking","catagory":"Art","subcatagory":"Potrait","title":"Potrait","item_condition":"Good","description":"potarit","address":"Blah St","city":"lalaland","state":"NA","country":"NA","zipcode":"blah","posted_on":"2012-05-27"}}
4

1 回答 1

3

mysql_fetch_array() 只将查询结果集中的单行作为数组返回。它不会获取所有行。由于您从 getFreebies 方法返回获取的结果,而不是结果句柄本身,因此调用代码不可能从查询中获取任何其他结果,除了您获取的一行。

因此,您应该拥有:

public function getFreebies() {
   $result = ...
   return($result);
}

$getAllFreebies = $db->getFreebies(...);
$data = array();
while($row = mysql_fetch_assoc($getAllFreebies)) {
   $data[] = array(
      .... your variable assignments here ...
   );
}
echo json_encod($data);
于 2012-05-29T21:25:23.560 回答