1

I‘m trying to create an android app where the data is filtered and fetched from a server. For that I want to upload a string to the script insert it into the query and fetch the rows that contain the given parameter. In my code I set a fixed string to test it, but the problem is that it doesn‘t return anything, maybe you can see my fault. The table in the database only contains rows of single letters

My script:

<?php

include 'DatabaseConfig.php';

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {

 die("Connection failed: " . $conn->connect_error);
} 

$var = $_POST['name'];

 $query = "SELECT * FROM TestTable WHERE name = '$var'";

    $result = mysqli_query($conn, $query);

    while($row = mysqli_fetch_assoc($result)) {
            $array[] = $row;
        } 

    header('Content-Type:Application/json');
    echo json_encode($array);
    mysqli_close($conn);

?>

My code:

public class MainActivity extends AppCompatActivity {

    List<GetDataAdapter> GetDataAdapter1;
    RecyclerView recyclerView;
    RecyclerView.LayoutManager recyclerViewlayoutManager;
    RecyclerView.Adapter recyclerViewadapter;
    ProgressBar progressBar;

    String a = "a";
    String ServerURL = "https://abcde.com/test.php";
    EditText name;
    Button button;
    String TempName;
    String JSON_ID = "id";
    String JSON_NAME = "name";

    JsonArrayRequest jsonArrayRequest;
    com.android.volley.RequestQueue requestQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        GetDataAdapter1 = new ArrayList<>();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        button = (Button) findViewById(R.id.button);
        recyclerView.setHasFixedSize(true);
        recyclerViewlayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(recyclerViewlayoutManager);
        name = (EditText) findViewById(R.id.editText);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                GetData();

                InsertData(a);

                JSON_DATA_WEB_CALL();


            }
        });
    }

    public void GetData() {

        TempName = name.getText().toString();


    }

    public void InsertData(final String a) {

        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {

                String NameHolder = a;


                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("name", NameHolder));


                try {
                    HttpClient httpClient = new DefaultHttpClient();

                    HttpPost httpPost = new HttpPost(ServerURL);

                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse httpResponse = httpClient.execute(httpPost);

                    HttpEntity httpEntity = httpResponse.getEntity();


                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
                return "Data Inserted Successfully";
            }

            @Override
            protected void onPostExecute(String result) {

                super.onPostExecute(result);

                Toast.makeText(MainActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();

            }
        }

        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();

        sendPostReqAsyncTask.execute(a);
    }


    public void JSON_DATA_WEB_CALL() {

        jsonArrayRequest = new JsonArrayRequest(ServerURL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        progressBar.setVisibility(View.GONE);

                        JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }

    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {

        for (int i = 0; i < array.length(); i++) {

            GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                GetDataAdapter2.setId(json.getInt(JSON_ID));

                GetDataAdapter2.setName(json.getString(JSON_NAME));


            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }

        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);

        recyclerView.setAdapter(recyclerViewadapter);
    }


}

1 Answer 1

1

You're doing wrong when you are receiving the data from databse like

while ($row = mysqli_fetch_assoc($result)) {
 // Now do like, name of the field in your database put inside  the $row['here'];
     echo $row['username'];
     echo $row['password'];
}

Now that should work; Thank's

Sign up to request clarification or add additional context in comments.

4 Comments

if you covert data to array you have to use implode();
When I set $var=3; for example it works and returns the correct data. Are you sure that it‘s the issue?
First practice for my code. you can not able to access data from database $array[] = $row; this is so wrong. $row[' name of fields like username'];
Okay, I will test as soon as possible. But why does it work for me that way as well?

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.