2

I have some problems getting data from local server to Android Application. The architecture: Android - PHP/Query - MySQL database.

This is the function (in loadData.php) calling the function (in db_function.php) which does the query:

     $exists = $db -> loadData($id);

In db_function.php the function looks like this:

public function loadData($app_cust_id) {

    $results = mysql_query("SELECT * FROM ...")  or die (mysql_error());    

         $messages = array();

            if (mysql_num_rows($results)) {

              while ($result = mysql_fetch_assoc($results)) {

              $messages[] = $result;

              }
            }
            return $messages;

...and now back...What do i have to write in order to get array result back and echo the right json_encode? Do i have to create a new array, loop through result and store it in the new array?

    $result = $db -> loadData($id);

        // ???
        $response["success"] = 0;
        echo json_encode($response);

EDIT :

    $result = $db -> loadData($id);
    $result["success"] = 0;
    echo json_encode($result);

So i did these changes. It seems like no data is sended back to my java function. The logcat says: "Error parsing data org.json.JSONException: End of input at character 0 of" I didn´t find a solution. Anyone who can tell me about handling with the right json_encode and the right jsonparser in java. thanks a lot!

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

2 Answers 2

2
$result = $db -> loadData($id);

What the loadData method returns is stored in your variable named $result and not $response. Hence it should be:

    // not $response["success"] =0; 
    $result["success"] = 0;
    echo json_encode($result);
Sign up to request clarification or add additional context in comments.

Comments

2

Personally, I would not mix your success element in the same array that has your messages. I would do something like this:

$response = array();
$messages = $db->loadData($id);
$response['success'] = 0;
$response['messages'] = $messages;
echo json_encode($response);

Now I noticed you have absolutely no error handling or edge case handling in your function (You just die if there is an error). What should you return to calling client if the query fails? What do you do if no messages are found?

Finally, a couple notes around how you are working with the DB:

  • You should not be using mysql_* functions as they are deprecated. I would suggest using mysqli or PDO.
  • Your function does nothing to guarantee that you do indeed have a database connection that is available within the scope of your function. You might consider passing in a resource handle to the DB connection to this function and then referencing that handle in the query function call.

3 Comments

You´re right. But it seems that code under the query with array is not working ... any idea? Because when i change this to get only one row of the result it´s working ...
@DehMotth json_encode() should bot give you invalid JSON format. It sounds to me like you simple need to debug your code to find out where your method is failing to behave as you expect. In other words, you need to define what "not working" means. Why is it not working? Where in the code do you get variable values that you do not expect?
how many columns are in your query's result? Since you are using select * then it might fluctuate so I would avoid that! Also is the first column of your result unique? it should be unique because it's potentially going to become a key for json so make it so in the query ...something like select COL1, COL2.... group by COL1. Also you can just assign $response['messages'] = $db->loadData($id) and skip the extra arrays ops which is always a good thing ceteris paribus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.