1

can someone help me what i suppose to do to fix this error? because, the result of the code is success for json format. but there's still have error "Undefined variable: query_params".

i want to make listview in my android program from this json format. and in my android it can't show the success json format from my comments.php. and i think, maybe because this error "Undefined variable: query_params ".

enter image description here

this is my comments.php

<?php


require("config.inc.php");

//initial query
$query = "Select * FROM comments";

//execute query
try {
$stmt   = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}


$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"]   = array();

foreach ($rows as $row) {
    $post             = array();
    $post["post_id"]  = $row["post_id"];
    $post["username"] = $row["username"];
    $post["title"]    = $row["title"];
    $post["message"]  = $row["message"];


    //update our repsonse JSON data
    array_push($response["posts"], $post);
}

// echoing JSON response
echo json_encode($response);


} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}

?>

this is my android coding to read the json format from comments.php to make listview. and nothing show for the listview.

ReadComments.java

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ReadComments extends ListActivity {


private ProgressDialog pDialog;


private static final String READ_COMMENTS_URL = "http://192.168.56.101/webservice/comments.php";


private static final String TAG_SUCCESS = "success";
private static final String TAG_TITLE = "title";
private static final String TAG_POSTS = "posts";
private static final String TAG_POST_ID = "post_id";
private static final String TAG_USERNAME = "username";
private static final String TAG_MESSAGE = "message";

private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mCommentList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // note that use read_comments.xml instead of our single_post.xml
    setContentView(R.layout.read_comments);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    // loading the comments via AsyncTask
    new LoadComments().execute();
}

public void addComment(View v) {
    Intent i = new Intent(ReadComments.this, AddComment.class);
    startActivity(i);
}


public void updateJSONdata() {


    mCommentList = new ArrayList<HashMap<String, String>>();
    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

    try {
        mComments = json.getJSONArray(TAG_POSTS);
        for (int i = 0; i < mComments.length(); i++) {
            JSONObject c = mComments.getJSONObject(i);


            String title = c.getString(TAG_TITLE);
            String content = c.getString(TAG_MESSAGE);
            String username = c.getString(TAG_USERNAME);


            HashMap<String, String> map = new HashMap<String, String>();

            map.put(TAG_TITLE, title);
            map.put(TAG_MESSAGE, content);
            map.put(TAG_USERNAME, username);


            mCommentList.add(map);


        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}


private void updateList() {
    ListAdapter adapter = new SimpleAdapter(this, mCommentList,
            R.layout.single_post, new String[] { TAG_TITLE, TAG_MESSAGE,
                    TAG_USERNAME }, new int[] { R.id.title, R.id.message,
                    R.id.username });

    setListAdapter(adapter);
    ListView lv = getListView();    
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        }
    });
}

public class LoadComments extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ReadComments.this);
        pDialog.setMessage("Loading Comments...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        updateJSONdata();
        return null;

    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        updateList();
    }
}
 }
LeanDev
  • 251
  • 6
  • 19

2 Answers2

2

You are seeing the "undefined variable" error because you are trying to use the variable $query_params in your execute() statement before defining what the variable should contain. See this answer.

The $query_params that you are sending to your execute() statement is also not needed, seeing how you are not using any parameters in your $query. See this PHP manual on PDO Statement.

So, to fix your error, just change this:

$result = $stmt->execute($query_params);

To this:

$result = $stmt->execute();
Community
  • 1
  • 1
Sutandiono
  • 1,748
  • 1
  • 12
  • 21
1

So, to fix your error, just change this:

$result = $stmt->execute($query_params);

To this:

$result = $stmt->execute();

When i change that as you said, i am getting this

{"success":0,"message":"Database Error!"}

Gachau Ny
  • 11
  • 3