dtihe8614 2012-07-12 21:55
浏览 41
已采纳

使用java(android)动态更改php中的mysql查询

I'm using a MySQL database and php for my java/android app. I haven't got any experience with php.

this is my php file (getAllDataFromSomeTable.php)

<?php
mysql_connect("someHosturl","someUsername","somePassword");
mysql_select_db("databasename");

$q=mysql_query("SELECT * FROM sometable");
while($e=mysql_fetch_assoc($q))
    $output[]= $e;

print(json_encode($output));

mysql_close();
?>

and i work with HttpPosts and stuff like that in Java. This way i can get all the data from 'sometable'

but if i want to use a different query like "select top 1 from sometable where username = 'thisuser'" for example. How can i change that dynamically in java? How should my php file look and how should the code in java look? this is the code i have now:

String result = "";
    List<? extends NameValuePair> licenses = (List<? extends NameValuePair>) new ArrayList<DriversLicense>();
    InputStream is = null;
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://some-url.com/getAllDataFromSomeTable.php");
        httpPost.setEntity(new UrlEncodedFormEntity(licenses));
        HttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.d("httpclient tag", e.getMessage());
    }

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "
");
        }
        is.close();

        result=sb.toString();
        Log.d("result is", result);
    }catch(Exception e){
         Log.d("log-tag", "Error converting result "+e.toString());
    }

    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Log.d("from jsonObject", "id= " + json_data.getInt("Id") + ", number = "
                        +json_data.getString("Number"));
        }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
  • 写回答

1条回答 默认 最新

  • dongyinglan8707 2012-07-12 22:06
    关注

    Why don't you try adding parametes to the request?

    For example:

    http://some-url.com/getAllDataFromSomeTable.php?table=difftable&whereField.1=username&whereValue.1=xyz&whereField.2=surname&whereValue.2=Smith

    This way you would need to parse these parameters and build a query based on the passed data. I'm thinking that the above request would make this query:

    select * from difftable where username = 'xyz' and surname ='Smith'
    

    On the other hand, this is what webservices are for, so i would think about something like that, if possible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题