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!