如何使用唯一键值检索 mysql 数据


How to retrieve mysql data using a unique key value?

我正在尝试使用php脚本从mysql数据库中检索用户的电子邮件ID来检索用户数据。但是我无法检索 json。

在这里,我附加了我的代码以及php脚本。帮助我解决这个问题。

protected String doInBackground(String... params) {
            String JSON_STRING;
            try {
                String user_email = params[0];
                URL url = new URL(get_json);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferwriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                String data = URLEncoder.encode("user_email", "UTF-8")+"="+URLEncoder.encode(user_email,"UTF-8");
                bufferwriter.write(data);
                bufferwriter.flush();
                bufferwriter.close();
                os.close();

                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                while ((JSON_STRING = bufferedReader.readLine()) != null) {
                    stringBuilder.append(JSON_STRING + "'n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

PHP代码:

<?php

$db_name="society_management";
$mysql_user="root";
$mysql_pass="";
$server_name="localhost";
$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);


$mail=$_POST["user_email"];

$sql = "select * from member where e-mail='$mail';";

$res = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($res))
{
array_push($response,$row[0],$row[1],$row[2],$row[3],$row[4],$row[5]);
}
echo json_encode(array('result'=>$response));
echo "hey " .$mail;
mysqli_close($con); 
?>

试试这段代码。希望它会有所帮助。

<?php
    $db_name="society_management";
    $mysql_user="root";
    $mysql_pass="";
    $server_name="localhost";
    $con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
    $mail=$_POST["user_email"];
    $sql = "select * from member where e-mail='$mail';";
    $res = mysqli_query($con,$sql);
    $response = array();
    while($row=mysqli_fetch_array($res))
    {
        array_push($response,$row);
    }
    echo json_encode(array('result'=>$response));
    echo "hey " .$mail;
    mysqli_close($con); 
?>