1

Now I have this:

$data = array();
$query = mysql_query("SELECT * FROM users WHERE username = '{$username}'");

while ($row = mysql_fetch_row($query)) {
$data[] = $row;
}

Now $data[0] = array('Joe','Sally','3 5'); $data[1] = array('Joe','alice','30 65');

Access $data[0][0] = 'Joe' or $data[1][0] = 'Joe'. $data[1][1] = 'alice'.

then how can i send this 2D array from php and receive it in android using JSONObject or JSONArray?

2
  • 1
    You have an sql injection problem ! (Imagine someone having $username ' or 1 -- ). Learn mysqli_* or PDO_* and use parameterized queries ... mysql_* is deprecated. Commented Dec 14, 2013 at 9:19
  • Not to mention the use of mysql_ functions ... Commented Dec 14, 2013 at 9:20

3 Answers 3

1

try this

$data = array();
$query = mysql_query("SELECT * FROM users WHERE username = '{$username}'");

while ($row = mysql_fetch_row($query)) {
$data[] = $row;
}
 $json=json_encode(array("userlist" => $data));
        echo $json;
Sign up to request clarification or add additional context in comments.

Comments

1

you can use json_encode() function to convert your array to json array.In your android development code,you have to add JSONParser to extract your information.Try this java class for getting information from PHP using JSON.

In PHP
$data = array();
$query = mysql_query("SELECT * FROM users WHERE username = '{$username}'");

while ($row = mysql_fetch_row($query)) {
$data[] = $row;
}

echo json_encode($data);

JSONParser.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } 
        catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

2 Comments

okay thank u but i already have the JSONParser class what i can't do is that i can't receive the 2D array i sent from php in my android activitiy class. how can i do this?
@user3101219:see my edited version.add json_encode($data).It will work fine.for example if u echo it it will be in the form [{"no":"101","name":"arun"},{"no":"102","name":"Mennakshi"},{"no":"103","name":"Santhosh"}].Just consider the format the example isrelated to my project
0

User json_encode($dd) to show Json String

$data = array();
$query = mysql_query("SELECT * FROM users WHERE username = '{$username}'");

while ($row = mysql_fetch_row($query)) {
$data[] = $row;
}

echo json_encode($data) ;

2 Comments

and then how to receive it in android in an array with the same format and then split it by the comma?
you get Json string array and need to parse this json string.

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.