Retrieving Data from DB

In order to retrieve data from the database and display it, you must have created a database and a table. Hence, it is advised that you first execute the Registration Form program because here we will be using the UserLoginTable, and DatabaseConfig.php created in the Registration Form program.

Create RegistrationDetails.php file inside public_html folder and write the following code.

<?php
include 'DatabaseConfig.php';

$email = isset($_POST['email']) ? $_POST["email"] : '';;

//creating a query
 
$sql="SELECT `email`, `name`, `contact`, `address` FROM UserLoginTable WHERE email like '%$email%'";

$r=mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($r)){
    array_push($result,array(
        'email' =>$row['email'],
        'name' =>$row['name'],
        'contact' =>$row['contact'],
        'address'=>$row['address']
        ));
}
echo json_encode(array('UserLoginTable'=>$result));
mysqli_close($con);
?>

Step 1: Start an android project.

Step 2: Design the first page as shown in the figure.

Step 3: Open the AndroidManifest.xml file and add the following line after the application tag.

<uses-permission android:name="android.permission.INTERNET" />

Step 4: Open build.gradle(Module:………..) file, inside dependencies, add the following code and synchronize the project.

implementation 'com.android.volley:volley:1.1.1

Step 5: Create a second page with the name Display_DB_Contents and design it as shown in the figure.

Step 6: Create a new java class get_ID_Details.java and write the following code. Note down the package name generated by the Android Studio which is in the first line (It is different for different programs). This package name will be used in Display_DB_Contents.java file (Used in line no 41).

package com.example.part_b7_display_db_content;
public class get_ID_Details {
    String id;
    private static final get_ID_Details ourInstance = new get_ID_Details();
    public static get_ID_Details getInstance() {
        return ourInstance;
    }
    private get_ID_Details() {
    }
    public void setData(String id) {
        this.id = id;
    }
    public String getData() {
        return id;
    }
}

Step 7: Write the following in Display_DB_Contents.java.

package com.example.part_b7_display_db_content;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Display_DB_Contents extends AppCompatActivity {

    TextView Name,Contact, Email, Address;
    String id_holder;
    String Name_Holder,Contact_Holder, Email_Holder, Address_Holder;
    Boolean found=false;

    String webhost_path="YOUR_WEBSITE_URL/RegistrationDetails.php";

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

        Name=(TextView)findViewById(R.id.Text_Name);
        Contact=(TextView)findViewById(R.id.Text_contact);
        Email=(TextView)findViewById(R.id.Text_email);
        Address=(TextView)findViewById(R.id.Text_address);

        get_ID_Details get_id_obj=com.example.part_b7_display_db_content.get_ID_Details.getInstance();
        id_holder=get_id_obj.getData();
        get_User_Details(id_holder);
    }
    private void get_User_Details(String id_holder){
        final StringRequest stringRequest1 = new StringRequest(Request.Method.GET, webhost_path,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response)
                    {
                        try
                        {
                            JSONObject obj = new JSONObject(response);
                            JSONArray heroArray = obj.getJSONArray("UserLoginTable");

                            for (int i = 0; i < heroArray.length(); i++)
                            {
                                JSONObject jsonObject = heroArray.getJSONObject(i);

                                Email_Holder = jsonObject.getString("email");
                                Name_Holder = jsonObject.getString("name");
                                Contact_Holder=jsonObject.getString("contact");
                                Address_Holder=jsonObject.getString("address");

                                if(Email_Holder.equals(id_holder)) {
                                    found=true;
                                    Email.setText(Email_Holder);
                                    Name.setText(Name_Holder);
                                    Contact.setText(Contact_Holder);
                                    Address.setText(Address_Holder);
                                }
                            }
                            if(!found)
                            {
                                Toast.makeText(getApplicationContext(),"User not Found",Toast.LENGTH_LONG).show();
                            }
                        }
                        catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        final RequestQueue requestQueue1 = Volley.newRequestQueue(Display_DB_Contents.this);
        requestQueue1.add(stringRequest1);
    }
}

Step 8: Open MainActivity.java and add the following code.

package com.example.part_b7_display_db_content;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btn_details;
    EditText email;
    public String email_holder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        email=(EditText)findViewById(R.id.editText_email);
        btn_details=(Button)findViewById(R.id.btn_get_details);


        btn_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                email_holder=email.getText().toString();

                get_ID_Details get_id_obj=com.example.part_b7_display_db_content.get_ID_Details.getInstance();
                get_id_obj.setData(email_holder);

                Intent in=new Intent(MainActivity.this,Display_DB_Contents.class);
                startActivity(in);
            }
        });
    }
}

Step 9: Run the project.