0
我遇到了我的PHP函數的問題,但我無法弄清楚在哪裏!有人可以幫助我嗎? java類記錄我Log: entry corrupt or truncated
,當我去解析json數據返回JSONException: End of input at character 0 of
。 從調試我看到它返回一個空字符串「」。來自PHP/mySQL服務器的空響應
這是PHP代碼:
...
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;
}
和Java代碼:
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;
}
的jsonParser和getUserById完美的作品與其他功能。那麼我錯在哪裏?
如何設置$標籤?我也看到,如果你沒有從數據庫的結果,你會返回數組而不是假,所以這個檢查:如果($以下)將永遠是真的 – bksi
@bksi $ tag ==「getFollowing」,所以它打開if塊。所以,如果它總是真的,爲什麼它甚至沒有設置成功= 1或以下=一個空陣列? –
請在編寫任何**更多SQL接口代碼之前,您必須閱讀[適當的SQL轉義](http://bobby-tables.com/php)以避免嚴重的[SQL注入漏洞](http: //bobby-tables.com/)。另外,'mysql_query'不應該在新的應用程序中使用。這是從PHP的未來版本中刪除的不推薦使用的界面。像[PDO這樣的現代替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)並且將讓你的數據庫代碼更容易得到正確的。 'intval'不能代替正確的轉義,這是一個柺杖。 – tadman