3

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());
    }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 ;
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.