2013-09-05 3 views
0

J'ai des problèmes avec ma fonction php mais je n'arrive pas à comprendre où! Quelqu'un peut m'aider? La classe java me connecte Log: entry corrupt or truncated et quand je vais analyser les données json retourne JSONException: End of input at character 0 of. À partir du débogage je l'ai vu renvoyer une chaîne vide "".Réponse vide de PHP/serveur MySQL

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

et le code 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; 
} 

le jsonParser et getUserById fonctionne parfaitement avec les fonctions des autres. Alors, où ai-je tort?

+0

Comment vous définissez $ tag? Aussi je vois que si vous n'avez pas de résultat de db, vous retournerez array au lieu de false, donc cette vérification: if ($ following) sera toujours vrai – bksi

+0

@bksi $ tag == "getFollowing" donc il ouvre le si bloc. Donc, si c'est toujours vrai, pourquoi il ne définit même pas success = 1 ou following = an-empty-array? –

+0

S'il vous plaît, avant d'écrire ** tout ** code d'interfaçage SQL plus, vous devez lire sur [échappement SQL approprié] (http://bobby-tables.com/php) pour éviter les graves [bogues d'injection SQL] (http: //bobby-tables.com/). De plus, mysql_query ne devrait pas être utilisé dans de nouvelles applications. C'est une interface obsolète qui est supprimée des futures versions de PHP. Un remplacement moderne comme [PDO n'est pas difficile à apprendre] (http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) et rendre le code de votre base de données plus facile à obtenir. 'intval' n'est pas un substitut pour s'échapper correctement, c'est une béquille. – tadman

Répondre

0

Je résolus de cette façon

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

Le problème était que nécessaire le $ cet appel

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