通过php检索sql数据库中的一行并将输出转换为android应用程序中的textView

i am really really new to android programming so pardon me for my total lack of knowledge

as of now, i have a sql database with 3 rows, "name", "contact" and "status"

I would like to retrieve both "status" whenever that matches the query of both a "name" and "contact". After getting the "status" query,I would like to convert the query into a TextView and display it in an android activity.

I still cant find out how to do so despite hours of searching.Thank you. My biggest problem is retrieving the "status" that matches "name" and "contact". Thank you for your help

This is my php script

<?php 
 

 $name = $_GET['name'];
 $contact = $_GET['contact'];


 require_once('Connect.php');
 

 $sql = "SELECT * FROM database WHERE name=$name and contact = $contact";
 

 $r = mysqli_query($con,$sql);
 

 $row = mysqli_fetch_assoc($r);
 $status =$row["status"];
 
 echo "Your Current Status: ".$status;
 
 
 mysqli_close($con);

EDIT:

My editted snippet is this, converted it to an array somehow and it kinda works.

$result = array();
 $row = mysqli_fetch_array($r);
 array_push($result,array(

 "status"=>$row['status'],
 ));
 
 //displaying in json format 
 echo json_encode(array('result'=>$result));

However, I have totally no idea how to use JSON to retrieve the status from the array and convert it to a String (and eventually TextView). Thank you,anyone for helping

</div>

Try

  $sql = "SELECT * FROM your_table WHERE name='$name' and contact = '$contact' ";

mind the single quote ' ' around your variables.

Use mysqli_error() function to find any errors.

Further make sure you use JSON to communicate with your server and the android client app.

For that you should have a Class called "JsonParser" insert below method.

    public JSONObject makeHttpRequest(String url, List<NameValuePair> params) {
        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            is = httpEntity.getContent();

        }catch(UnsupportedEncodingException e){
            Log.e(tag + "-> EncodingError", e.toString());
            e.printStackTrace();
        }catch(ClientProtocolException e){
            Log.e(tag + "-> ProtocolError", e.toString());
            e.printStackTrace();
        }catch(IOException e){
            Log.e(tag + "-> IOError", e.toString());
            e.printStackTrace();
    }

    try{
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;

        while((line = br.readLine())!= null){
            sb.append(line + "
");
        }
        is.close();
        json = sb.toString();
        Log.i(tag, "-> JSON String Buffer Created");

    }catch(Exception e){
        Log.e(tag + "-> Buffer Error", "Conversion Error" + e.toString());
    }

    try{
        Log.i("tagconvertstr", "["+json+"]");
        jobj = new JSONObject(json);
        Log.i(tag, "-> JSON object Created");

    }catch(JSONException e){
        Log.e(tag + "-> JSON Parser", e.toString());
    }

    return jobj;
}

Next initialize your Textview

    private Textview txtviw;

    JSONObject jobj = null;
    JsonParser jParser = new JsonParser();

In onCreate()

    txtviw = (Textview ) findViewById(R.id.your_text_view_id);

Then in an AsyncTask class call this php file and get the json object created.

In doInBackground()

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

        params.add(new BasicNameValuePair("name", your_name));
        params.add(new BasicNameValuePair("contact", your_contact));

        jobj = jParser.makeHttpRequest(YOUR_URL_TO_PHP_FILE, params);

finally in onPostExecute()

    txtviw.setText(jobj.getString("status"));

You can learn more about AsyncTask from google. Further it is a best practice to use AsyncTask wen communicating with databases.

There are two obvious errors in your code - you are selecting something from database instead of table. Second one is wrong SQL query for string variables and missing single quotation marks, so this should be correct code:

<?php 
    $name = $_GET['name'];
    $contact = $_GET['contact'];
    require_once('Connect.php');
    mysqli_select_db($con, 'database') or die(mysqli_error($con));
    $sql = "SELECT * FROM `table` WHERE `name` = '$name' AND `contact` = '$contact'";
    $r = mysqli_query($con, $sql) or die(mysqli_error($con));
    $row = mysqli_fetch_array($r, MYSQL_ASSOC);
    $status = $row["status"];
    echo "Your Current Status: ".$status;
    mysqli_close($con);
 ?>

Last but not least is a good practice to use mysqli_error() function together with any call to your database in order to inspect any errors.