0

Hello everybody I'm doing a client/server app in android. What I want to do is to send data from client then the server retrieves and send again to client..

For example I'm going to send the item code to my server and I will like to query something like this:

 SELECT itemname FROM items WHERE itemcode = :itemcode $itemname = $result['itemname']; 

Where :itemcode is the data that's being passed by my client. Next, after passing the itemcode, I would like to use the itemname to include in my next query to retrieve all other records.

SELECT itemsize, itemmeasure FROM itemsdetail WHERE itemname = :itemname 

And so it displays all the records.

What I've tried so far, I created another table to query the last inserted data. But I think this is not efficient. I only want to put the queries in one php file.

<?php include('dbconnect.php'); $sql = "SELECT itemname FROM items ORDER BY _id DESC LIMIT 1"; $stmt = $conn->prepare($sql); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); $itemname = $result['itemname']; $sql = "SELECT itemsize, itemmeasure FROM itemsdetail AND itemname = :itemname"; $stmt = $conn->prepare($sql); $stmt->execute(array(':itemname' => $itemname)); $set = array(); while($r = $stmt->fetchAll(PDO::FETCH_ASSOC)){ $set = $r; } header('Content-type: application/json'); echo json_encode($set); ?> 

Java:

@Override protected Void doInBackground(Void... params) { // Create the array arraylist = new ArrayList<HashMap<String, String>>(); // Retrieve JSON Objects from the given website URL in JSONfunctions.class String result = JSONFunctions.getJSONfromURL(URL); try { JSONArray jr = new JSONArray(result); for(int i=0;i<jr.length();i++) { HashMap<String, String> map = new HashMap<String, String>(); jb = (JSONObject)jr.get(i); map.put(TAG_ITEMSIZE, jb.getString(TAG_ITEMSIZE)); map.put(TAG_ITEMMEASURE, jb.getString(TAG_ITEMMEASURE)); arraylist.add(map); } } catch (JSONException e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return null; } 

So far I'm getting error if I put the two queries in one PHP file, I don't know what's wrong. So my alternative solution is to create another php file to receive the passed data from my client. But I know this is not a good solution. I need to be able to put it inside one php file to pass my data and use that passed data inside my second query. Do you have any ideas how to do this? Your help will be greatly appreciated. thanks.

2 Answers 2

1

After chatting on Google Plus, the root cause was due to the tutorial you are using with JSONParser. The JSONParser seems like a third party API, yucky...

You are getting back an Array from your server instead of an Object which caused the problem. Here is the implementation code shared on Google Plus

// Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(URL); try { // Building Parameters List<NameValuePair> params1 = new ArrayList<NameValuePair>(); params1.add(new BasicNameValuePair(TAG_VANCODE, "VAN-ABC")); // Set HTTPPOST with your parameters httppost.setEntity(new UrlEncodedFormEntity(params1)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); // Setup an InputStream to handle the response. InputStream ips = response.getEntity().getContent(); BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"iso-8859-1"), 8); // Use a reader to read the data and store it in a String builder StringBuilder sb = new StringBuilder(); String s; while(true) { s = buf.readLine(); if(s==null || s.length()==0) break; sb.append(s); } buf.close(); ips.close(); System.out.println("I AM HERE TESTING OUTPUT - " + sb.toString()); // Take the JSON and create an array // sb.string should look like this JSONArray jr = new JSONArray(sb.toString()); for(int i = 0; i<jr.length(); i++) { JSONObject jo = jr.getJSONObject(i); System.out.println("Document Number " + jo.getString("doccumentnumber")); System.out.println("Item Code: " + jo.getString("itemcode")); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try

SELECT itemsize, itemmeasure FROM itemdetails INNER JOIN items ON itemdetails.itemname = items.itemname WHERE items.itemcode = #itemcode 

I'm not very good in MySQL, may be you have to put some alias but if the idea is to make only one query this should work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.