1

I need to read data from an external database with thousands of items. What I did so far is that I wrote the php query which returns all the items, then I parse them with JSON. Like this:

php:

   $stmt = $conn->prepare("SELECT COMMENT_ID, USERNAME, COMMENT, DATE, LINK, NOW() AS SERVERTIME FROM COMMENTROOM ORDER BY DATE DESC");
   $stmt -> execute(array($link));
   while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
        $output[] = $row;
   }
   print(json_encode($output));  

android:

    JSONArray jArray = new JSONArray(commentResult);

    for(int i=0;i<jArray.length();i++){
      JSONArray innerJsonArray = jArray.getJSONArray(i);
      for(int j=0;j<innerJsonArray.length();j++){
        JSONObject jsonObject = innerJsonArray.getJSONObject(j);

        if (jsonObject.getString("LINK").equals(GlobalVars.getLINK_FOR_COMMENT())){
            Comment.add(jsonObject.getString("COMMENT"));
        }
      }
    }

This works great, but I don't think this is effective especially if the table contains tens of thousands of items. So somehow I would like to pass the link variable to the php query, so the selecting process will be done by sql and not via the JSON parsing which is a lot slower. The new php:

$link = $_POST['link'];
    try {
        $stmt = $conn->prepare("SELECT COMMENT_ID, USERNAME, COMMENT, DATE, NOW() AS SERVERTIME FROM COMMENTROOM WHERE LINK=? ORDER BY DATE DESC");
        $stmt -> execute(array($link));
        while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
            $output[] = $row;
        }
        print(json_encode($output));

My question is how can I pass the link variable from android to this php query so the query will only return the proper items and I dont have to parse the result?

Thanks in advance!

Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • How big it that `link`? Isn't it something you can pass as a `GET`/`POST` parameter? – gunar Jun 30 '13 at 14:38
  • You need to pass that parameter before sending request to URL. Check this question how to add post parameters to http request http://stackoverflow.com/questions/2938502/sending-post-data-in-android – andrew Jun 30 '13 at 14:40
  • Okay, I can post a variable, to php, but how can I do the query at the same tame? – Jani Bela Jul 01 '13 at 10:27
  • The problem is that I make a HTTP request when I post and as I wrote above I create another when I do the query and read the data int oa strinbuilder. How can I do it with one request? – Jani Bela Jul 01 '13 at 10:39

0 Answers0