phpandroidmysql

Retrieving multiple rows from mysql


Hi I am trying to retrieve multiple rows of data from the database but whenever I run my app the app crashes.

In my case, I want to retrieve rows with the user name from the database.

   protected String doInBackground(String... args) {    
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            // getting JSON string from URL
            JSONObject json = jsonParser.
                    makeHttpRequest(url_all_coupons, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                coupons = json.getJSONArray(TAG_COUPONS);

                // looping through All Products
                for (int i = 0; i < coupons.length(); i++) {
                    JSONObject c = coupons.getJSONObject(i);

                    // Storing each json item in variable
                    String couponcreated = c.getString(TAG_COUPONCREATED);
                    String couponexpires = c.getString(TAG_COUPONEXPIRES);
                    String coupondetails = c.getString(TAG_COUPONDETAILS);

This is my log cat

 E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

 3961-4003/info.androidhive.loginandregistration E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at info.androidhive.loginandregistration.CouponPageActivity$LoadAllCoupons.doInBackground(CouponPageActivity.java:98)
            at info.androidhive.loginandregistration.CouponPageActivity$LoadAllCoupons.doInBackground(CouponPageActivity.java:71)


            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)

This is my php code

$result = mysql_query("SELECT * FROM coupons WHERE name = '$name'") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();

while ($row = mysql_fetch_array($result)) {
    // temp user array
    $product = array();
    $product["couponcreated"] = $row["couponcreated"];
    $product["couponexpires"] = $row["couponexpires"];
    $product["coupondetails"] = $row["coupondetails"];


    // push single product into final response array
    array_push($response["products"], $product);
}
// success
$response["success"] = 1;

Solution

  • you may be try this it will help you.

    $result = mysql_query("SELECT * FROM coupons WHERE name = '$name'") or   die(mysql_error());
    
    // check for empty result
    if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();
    
    while ($row = mysql_fetch_array($result)) {
    // temp user array
    $product = array();
    $product["couponcreated"] = isset($row["couponcreated"])?$row["couponcreated"]:'';
    $product["couponexpires"] = isset($row["couponexpires"])?$row["couponexpires"]:'';
    $product["coupondetails"] = isset($row["coupondetails"])?$row["coupondetails"]:'';
    
    
    // push single product into final response array
    array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;