【问题标题】:Passing string to PHP file to query SELECT statement将字符串传递给 PHP 文件以查询 SELECT 语句
【发布时间】:2016-04-07 00:55:38
【问题描述】:

我正在尝试将一个字符串(例如“名称”)传递给我的 PHP 脚本,该脚本需要通过 $_POST 从 Android 应用程序获取“名称”,然后在 SELECT 查询中使用它并将其传递回安卓应用。

除了我只能将字符串传递给 PHP 脚本但我不能在 SELECT 查询中使用它之外,我已经完成了所有工作。这是我的代码:

PHP 代码:

<?php
include "../dbc.php";
//include "get_id.php";

$value = $_POST["value"];
$response = array();

$q=$dbh->prepare("SELECT * FROM users WHERE user_name = :value");
$q->bindParam(':value', $value);
$q->execute();
while ($row = $q->fetch(PDO::FETCH_ASSOC))
{
    $first_name=$row['first_name'];
    $last_name=$row['last_name'];
    $company_name=$row['company_name'];
    $loc=$row['loc'];
    $tel=$row['tel'];
    $website=$row['website'];
    $aboutme=$row['aboutme'];
    array_push($response, array("first_name"=>$first_name,"last_name"=>$last_name,"company_name"=>$company_name,"loc"=>$loc,"tel"=>$tel,"website"=>$website,"aboutme"=>$aboutme));
}

echo json_encode(array("server_response"=>$response));
?>

安卓代码:

class GetUserSettings extends AsyncTask<Void,Void,String> {
    String json_get_settings_url;
    @Override
    protected void onPreExecute() {
        json_get_settings_url = "http://url.com/json.php";
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(Void... Voids) {
        URL url = null;
        try {
            url = new URL(json_get_settings_url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            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 (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String JSON_STRING) {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        String value = settings.getString("user_name", "");
        //this is where it starts parsing the JSON from the URL
        try {
            JSONObject jsonResponse = new JSONObject(JSON_STRING);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("server_response");

            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                user_name = jsonChildNode.optString(value);
                et_user_name.setText(value);
                first_name = jsonChildNode.optString("first_name");
                et_first_name.setText(first_name);
                last_name = jsonChildNode.optString("last_name");
                et_last_name.setText(last_name);
                company_name = jsonChildNode.optString("company_name");
                et_company_name.setText(company_name);
                loc = jsonChildNode.optString("loc");
                et_loc.setText(loc);
                tel = jsonChildNode.optString("tel");
                et_tel.setText(tel);
                website = jsonChildNode.optString("website");
                et_website.setText(website);
                aboutme = jsonChildNode.optString("aboutme");
                et_aboutme.setText(aboutme);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"Something is wrong. Please restart the app or contact us.",Toast.LENGTH_LONG).show();
        }
    }
}

所以从代码中,如您所见,我试图将字符串“值”传递给我的 PHP 代码,然后将“值”分配给 $value,然后在 SELECT 查询中使用 $value,但我该怎么做传递字符串“值”,然后执行查询,以便我可以使用 JSON 解析数据?

【问题讨论】:

  • 您的 Android 代码没有向您的 php 脚本发布任何内容。所以 $_POST 数组是空的。开始调整您的 php 代码以检查是否需要输入参数/数据。如果没有,则将相关信息回显到 Android 客户端。

标签: php android mysql arrays json


【解决方案1】:

您可以使用 ContentValues() 将字符串传递给 php 并返回选择查询。

ContentValues 值 = 新的内容值(); values.put("字符串",变量);

您可以将 url 传递为

http://example.com/dir/page.php?var=variable

在php方面

$var = $_GET['变量']: 现在您可以在选择查询中传递此 $var。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-16
    • 2011-04-20
    • 2020-04-23
    • 2017-10-08
    • 2013-07-03
    • 2017-11-15
    • 2012-04-14
    • 2012-06-25
    相关资源
    最近更新 更多