1

im trying to get some data from my DB.

My problem is that i get null pointer on my array when i try to get the data.

When i run my sql command in phpmyadmin i get good results.

I also checked with echo that $ids[$i] isnt null.

The error message:

Warning: array_push() expects parameter 1 to be array, null given

Thanks for helping.

This is my code:

while($i <  $size)
    { 

    $mysqli = new mysqli(****);

    $sql = "SELECT workout_name, user, likes, dislikes, date FROM workouts_wall WHERE id_for_wall = ? "; 

    $stmt = $mysqli->prepare($sql) or trigger_error($mysqli->error."[$sql]");

    $stmt->bind_param('i', $ids[$i]);

    $stmt->execute();

    $stmt->store_result();

    $stmt->bind_result($workout_name, $user, $likes, $dislikes, $date);

    if($stmt->fetch())
    {   
       // temp user array
       $workouts = array();

        $workouts["workout_name"] = $workout_name;
        $workouts["picture"] = getPicture($user);
        $workouts["user"] = $user;
        $workouts["likes"] = $likes;
        $workouts["dislikes"] = $dislikes;
        $workouts["date"] = $date;

        // push single product into final response array
        array_push($response["workouts"], $workouts);
    }

    $i++;
}

1 Answer 1

3

Your variable $response["workouts"] itself is not instantiated as an array, so array_push does not recognize it as an array. Try adding this before your while loop:

$response["workouts"] = array();

Since you're only adding a single element at a time to the array, it's easier and simpler to just use the following code (instead of the array_push call):

$response["workouts"][] = $workouts;
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it. and now i get this error on my android app: Error parsing data org.json.JSONException: Value 0 of type java.lang.Integer cannot be converted to JSONObject 0 Why it thinks its an int array?
@dasdasd I have no idea what's going on in your Android app, but that's the subject for another question, as your Java code may have separate issues of its own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.