2013-09-05 3 views
0

PHP 함수에 문제가 있습니다. 그러나 어디에 있는지 알 수 없습니다! 누군가 나를 도울 수 있니? Java 클래스는 Log: entry corrupt or truncated을 기록하고 json 데이터를 파싱하면 JSONException: End of input at character 0 of이 반환됩니다. 디버그에서 보니 빈 문자열 ""이 반환되었습니다.PHP/mySQL 서버의 응답이 없습니다

... 
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; 
} 

및 자바 코드 :

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 다른 기능이 완벽하게 작동

은 PHP 코드입니다. 그래서 내가 어디 잘못 됐어?

+0

? 또한 만약 당신이 데이터베이스에서 결과가 없으면 false 대신 배열을 반환 할 것이므로 다음과 같이 확인하십시오 : if ($ following)는 항상 true가 될 것입니다 – bksi

+0

@bksi $ tag == "getFollowing"그래서 if를 엽니 다. 블록. 그래서, 그것이 항상 사실이라면 왜 성공 = 1을 설정하지 않거나 빈 배열을 따르지 않는 것일까? –

+0

** 모든 SQL 인터페이싱 코드를 작성하기 전에 심각한 [SQL 주입 버그]가 발생하지 않도록 [올바른 SQL 이스케이프] (http://bobby-tables.com/php)를 읽어야합니다 (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

답변

0

나는 문제는 그 필요했다 이런 식으로

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; 
    } 
} 

에서 해결 $이 호출은 $ 태그를 설정하는 방법

$followed = $this->getUserById($followed_id);