Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 + "\n");
        }
        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());
    }
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks, i'll try it out, it's probably gonna work that way, but like i said, i have no experience in webservices or php :p tnx for the quick respond. But how should i write the query in the php file? can i put something in there like "$query" and than in java "someurl.com/getda...table.php?query=" + queryString ; –  dumazy Jul 12 '12 at 22:13
    
Yes, that's possible. Then in php, you can get the query parameter of url with: $_GET['query'] I'm not sure thought about security stuff, if some finds out this URL, then your DB can be easily overloaded or anything. –  Zsolt János Jul 12 '12 at 22:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.