检索Android数据并将其传递给PHP WHERE子句

时间:2015-07-26 14:06:06

标签: java php android

正如标题所说。我应该在我的java代码中包含哪些代码,我可以检索并将其传递给我的PHP。

这是我的PHP代码

<?PHP
include_once("connection.php");

if(isset($_POST['TableNum'])){ $TableNum = $_POST['TableNum']; } 
$TableNum = null;
$sql = "SELECT Name, Price, orderlist.Quantity FROM menu INNER JOIN orderlist ON orderlist.MenuID=menu.MenuID WHERE orderlist.TableNum='+$TableNum+' "; 
$result = mysqli_query($con,$sql);
$json = array();

if(mysqli_num_rows($result)){
    while($row=mysqli_fetch_assoc($result)){
        $json['menu'][]=$row;
    }
}
mysqli_close($con);
echo json_encode($json);

这是我的Java代码

我需要将此变量作为我的where子句在我的php脚本中传递。

final TextView idtable = (TextView)findViewById(R.id.lblTableNum);

    final TextView idtable = (TextView)findViewById(R.id.lblTableNum);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Orderlist.this);
    String aa = getResources().getString(R.string.Table_Num);
    String tableID = sp.getString(getString(R.string.Table_Num), aa);
    idtable.setText(tableID);

    ListView lv = (ListView) findViewById(R.id.listView1);      
    String url = "http://192.168.123.1/1purple/readOrder.php";
    try {
        JSONParser jParser = new JSONParser();
        JSONObject table = jParser.getJSONFromUrl(url);
        JSONArray data = table.getJSONArray("menu");
        //JSONArray data = new JSONArray(getJSONUrl(url));
        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("Name", c.getString("Name"));
            map.put("Quantity", c.getString("Quantity"));
            map.put("Price", c.getString("Price"));
            MyArrList.add(map);
        }
        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(Orderlist.this, MyArrList, R.layout.orderlist_arrangement,
                new String[] {"Name", "Quantity", "Price"}, new int[] {R.id.textView1, R.id.textView2, R.id.textView3});      
        lv.setAdapter(sAdap);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.e("Log", "Failed" +e);
        e.printStackTrace();
    }           
 }

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,因为我已经选择在Android部分使用凌空库,它帮助我将输入变量传递给php where子句  参考这篇文章并检查答案:

How to parse multiple rows with jsonOject

或遵循名为函数StoreUserInfo 的后续教程监视功能,并参考第二部分到名为 RegisterActivity.java 的活动,这些可以帮助您使用where子句获取:

https://www.androidtutorialpoint.com/androidwithphp/android-login-and-registration-with-php-mysql/