我已经使用url将我的应用程序与我的数据库连接到php。但是,我有一个文本字段和一个按钮。文本字段应该接受输入,然后将用于mysql命令"从表中选择" 。我怎么能做到这一点?
这是我现在的代码:
public List<Si> getData(){
data = new ArrayList<>();
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
String url = "http://10.0.2.2/functions/return_locations.php";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
if (response.length() > 0) {
data.clear();
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
Si info = new Si();
if (!jsonObject.isNull("name")) info.title = jsonObject.getString("name");
data.add(info);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("database", "no locations returned");
}
});
requestQueue.add(jsonArrayRequest);
return data;
}
这是我现在的PHP代码:
<?php
header('Content-Type: application/json ');
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = 'pass123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM Locations';
mysql_select_db('inscope');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($rows=mysql_fetch_assoc($retval)){
$json[] = $rows;
}
echo json_encode($json);
mysql_close($conn);
?>
答案 0 :(得分:0)
您可以将其作为GET参数添加到网址中,如下所示:
String url = "http://10.0.2.2/functions/return_locations.php?input=" + userInput;
在php中获取该代码在sql查询之前添加此代码
$input = $_GET['input'];
获得另一个输入:
$input2 = $_GET['input2']; // Note that you enter the string inside the GET function in the url
然后你的网址应如下所示:
String url = "http://10.0.2.2/functions/return_locations.php?input=" + userInput + "&input2=" + input2;