2

I want to store data of particular user from mysql to sqlite. I am only able to recieve only one row of particular user.

Here is my code.

login

JSONObject jsonexercise = userFunction.exerciseItems(name);

try {
    System.out.println(" exe try");
    if (jsonexercise.getString(KEY_SUCCESS) != null) {
        String result = json.getString(KEY_SUCCESS); 
        System.out.println(" exersise result" + result);
        if(Integer.parseInt(result) == 1){
            System.out.println("exercise inside parse");
            JSONObject json_exercisedata = json.getJSONObject("getexercise");
            db.exerciseItem(json_exercisedata.getString(KEY_ENAME),json_exercisedata.getString(KEY_EXERCISE_NAME),json_exercisedata.getString(KEY_DURATION),json_exercisedata.getString(KEY_CALORIE_BURNED),json_exercisedata.getString(KEY_ECREATED_AT));
        }
    } else {
        System.out.println("no exercise item to show");
    }
}
catch (JSONException e) {
System.out.println("no exerc item to show");

}

userfunction

public JSONObject exerciseItems(String name){
    // Building Parameters

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", get_exetag));

    params.add(new BasicNameValuePair("name", name));
    System.out.println("in json object name" + name);

    // getting JSON Object
    JSONObject jsonexercise = jsonParser.getJSONFromUrl(insertURL, params);
    // return json
    return jsonexercise;
}

dbhandler

public void exerciseItem(String name, String exercisename,String duration, String calorieburned, String created_at) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name
    System.out.println("in db handler insert user the name is" + name);
    values.put(KEY_EXERCISENAME, exercisename); // Email
    values.put(KEY_DURATION, duration); 
    values.put(KEY_CALORIE_BURNED, calorieburned);
    values.put(KEY_CREATED_AT, created_at); // Created At
    // Inserting Row
    db.insert(EXERCISE_ITEM, null, values);
    db.close(); // Closing database connection
}

php

public function getExeciseitems($name) {
    $result = mysql_query("SELECT name,exercisename,duration,calorieburned,created_at FROM exercisedata WHERE name = '$name'") or die(mysql_error());
    // check for result 
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysql_fetch_array($result);
        return $result;
    } else {
        // user not found
        return false;
    }
}
Musterknabe
  • 5,763
  • 14
  • 61
  • 117
pratz9999
  • 539
  • 4
  • 20

1 Answers1

1
if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);

                return $result;

        } else {
            // user not found
            return false;
        }

Does not work because you have more than one record extracted. You need to loop like this:

if ($no_of_rows > 0) {
while ($row = mysql_fetch_array($result)){
 $result[] = $row;
}
return $result;
}else {
                // user not found
                return false;
            }

The code is untested.

Edit: to avoid that error this link PHP - cannot use a scalar as an array warning Say to use:

if ($no_of_rows > 0) {
    $i = 0;
    $result = array();
    while ($row = mysql_fetch_array($result)){
     $result[$i] = array();
     $result[$i] = $row;
     $i++;
    }
    return $result;
    }else {
                    // user not found
                    return false;
                }

I've added a counter for populating. ($i)

edit2: changed the name:

if ($no_of_rows > 0) {
        $i = 0;
        $result2 = array();
        while ($row = mysql_fetch_array($result)){
         $result2[$i] = array();
         $result2[$i] = $row;
         $i++;
        }
        return $result2;
        }else {
                        // user not found
                        return false;
                    }
Community
  • 1
  • 1
Marco Mura
  • 582
  • 2
  • 13