当前位置: 动力学知识库 > 问答 > 编程问答 >

Empty response from PHP/mySQL server

问题描述:

i'm having problems with my php function but i can't figure out where! Someone can help me? The java class log me Log: entry corrupt or truncated and when i go parsing the json data returns JSONException: End of input at character 0 of.

From the debug i saw it return an empty string "".

This is the php code:

...

if ($tag == 'getFollowing') {

// Request type is getFollowing

$user_id = intval($_POST['user_id']);

$following = $db->getFollowing($user_id);

if ($following) {

//following get successfully

$response['success'] = 1;

$response['following'] = $following;

echo json_encode($response);

} else {

// following failed

$response['error'] = 1;

$response['error_msg'] = 'Error occured in getting following';

echo json_encode($response);

}

} else {

echo "Invalid Request";

}

...

?>

...

/**

* returns followings

*/

public function getFollowing($follower_id) {

$result = mysql_query("SELECT followed_id FROM follows WHERE follower_id = '$follower_id'");

$no_of_rows = mysql_num_rows($result);

$return_arr = array($no_of_rows);

if ($no_of_rows > 0) {

while ($row = mysql_fetch_array($result)) {

$followed_id = $row['followed_id'];

$followed = getUserById($followed_id);

array_push($return_arr, $followed);

}

}

return $return_arr;

}

and java code:

public List<JSONObject> getUserFollowing(String user_id) throws JSONException {

jsonParser = new JSONParser();

// Building Parameters

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("tag", getFollowing_tag));

params.add(new BasicNameValuePair("user_id", user_id));

// getting JSON Object

JSONObject json = jsonParser.getJSONFromUrl(followURL, params);

JSONArray arrayUsers = (JSONArray) json.get("following");

List<JSONObject> users = new ArrayList<JSONObject>();

for (int i=0; i<arrayUsers.length(); i++){

JSONObject jObj = arrayUsers.getJSONObject(i);

users.add(jObj);

}

// return json

return users;

}

the jsonParser and getUserById works perfectly with others functions. So where am i wrong?

网友答案:

I resolved in this way

 public function getFollowing($follower_id) {
    $return_arr = array();
    $result = mysql_query("SELECT * FROM follows WHERE follower_id = '$follower_id'");

    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {

        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $followed_id = $row['followed_id'];
            $followed = $this->getUserById($followed_id);
            array_push($return_arr, $followed);
        }
        return $return_arr;
    } else {
        // user not found
        return false;
    }
}

The problem was that is necessary the $this call

$followed = $this->getUserById($followed_id);
分享给朋友:
您可能感兴趣的文章:
随机阅读: