如何从MySQL发布并从MySQL app获取特定id的数据

时间:2014-11-22 21:28:03

标签: php android mysql

如何从MySQL加载特定id的数据。我的意思是当我点击gridview项目1来从MySQL加载我需要的这个项目时。目前我有

    $objConnect = mysql_connect("localhost","user","pass");
$objDB = mysql_select_db("database");
$strSQL = "SELECT * FROM table";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
    $arrCol = array();
    for($i=0;$i<$intNumField;$i++)
    {
        $arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
    }
    array_push($resultArray,$arrCol);
}

mysql_close($objConnect);

echo json_encode($resultArray);

这显然加载了该表中的所有内容。但是,如果我想点击列表item_1以加载select * from table where clickeditem = 1之类的查询,该怎么办? 我想我必须将它发布到.php脚本但是如何?

我不知道JAVA代码的哪一部分需要,所以如果您需要请告诉我,我会发布。 更新:     @覆盖         protected Void doInBackground(String ... params){             // TODO自动生成的方法存根

        String url = "file.php";

        JSONArray data;
        try {
            resultServer = getJSONUrl(url);
            data = new JSONArray(resultServer);

            MyArrList = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map;

            for(int i = 0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);
                map = new HashMap<String, Object>();
                map.put("id", (String)c.getString("id"));
                map.put("name", (String)c.getString("name"));
                map.put("price", (String)c.getString("price"));
                map.put("weight", (String)c.getString("weight"));

                // Thumbnail Get ImageBitmap To Bitmap
                map.put("ImagePathThum", (String)c.getString("image"));
                map.put("ImageThumBitmap", (Bitmap)loadBitmap(c.getString("image")));

                // Full (for View Full)
                map.put("ImagePathFull", (String)c.getString("image_big"));

                MyArrList.add(map);
            }


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

使用sql更新。

    $strSQL = "SELECT m.id, name, image, image_big, weight, price 
        FROM posts m 
        JOIN packet_r mr 
        ON m.id = mr.id 
        WHERE packed_id = 1";

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你确实需要将它发布到php脚本。这很容易:

static String url = "https://yourlink.com/database.php?clickeditem=1";

然后你可以编辑你的PHP代码,如:

$clickeditem = $_GET['clickeditem'];
$strSQL = "SELECT * FROM table WHERE clickeditem = '$clickeditem'";

要在java代码中编辑它,只需添加:

"https://yourlink.com/database.php?clickeditem=" + variable;

指向单击特定gridview项时要设置的变量的链接。