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; } } 
2
  • So you got your user from mysql, the problem is? Commented Dec 12, 2014 at 10:34
  • i am having more then 10 rows in mysql of particular user. I am only able to see one in my json response Commented Dec 12, 2014 at 10:35

1 Answer 1

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; } 
Sign up to request clarification or add additional context in comments.

6 Comments

PHP - cannot use a scalar as an array warning. But the good thing is it showing me the error as much time the record is available in database. What to do now ?
again same error on $result[$i] = $row; $i++;
after $i =0; you can add a line which says: $result = array();
It says mysql_num_rows() expects parameter 1 to be resource, array given in /home/bharatwellness/public_html/DB_Functions.php on line 73
@pratz9999 Change the $result used inside the $no_of_rows if with another name (updated the answer)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.