0

On backend I have rest api, the code is:

$app->get('/tasks', 'authenticate', function() {
            global $user_id;
            $response = array();
            $db = new DbHandler();

            // fetch task
            $result = $db->getAllUserTasks($user_id);

            $result = $db->getAllUserTasks($user_id);

        if ($result != NULL) {

            $response["error"] = false;
            $response["id"] = $result["id"];
            $response["task"] = $result["task"];
            $response["status"] = $result["status"];
            $response["createdAt"] = $result["created_at"];
            echoRespnse(200, $response);
        } else {
            $response["error"] = true;
            $response["message"] = "The requested resource doesn't exists";
            echoRespnse(404, $response);
        }
        });

and with this code I get just first data from array:

{
error: false
id: 2
task: "Create something"
status: 0
createdAt: "2014-12-01 01:58:42"
}

now when I want to fetch all data I write:

if ($result != NULL) {
                foreach ($result as $rez) {
                $response["error"] = false;
                $response["id"] = $rez["id"];
                $response["task"] = $rez["task"];
                $response["status"] = $rez["status"];
                $response["createdAt"] = $rez["created_at"];
                echoRespnse(200, $response);
                }
            } else {
                $response["error"] = true;
                $response["message"] = "The requested resource doesn't exists";
                echoRespnse(404, $response);
            }

so the same code with foreach loop but then I get: Unexpected token {

What is wrong with that? How I can implement here WHILE loop instead foreach ???

UPDATE with function:

public function getAllUserTasks($user_id) {
        $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
        $stmt->bind_param("i", $user_id);
        if ($stmt->execute()) {
            $res = array();
            $stmt->bind_result($id, $task, $status, $created_at);
            // TODO
            // $task = $stmt->get_result()->fetch_assoc();
            $stmt->fetch();
            $res["id"] = $id;
            $res["task"] = $task;
            $res["status"] = $status;
            $res["created_at"] = $created_at;
            $item[] = $res;
            $stmt->close();
            return $item;
        } else {
            return NULL;
        }
    }
2
  • Where exactly do you get "Unexpected token {"? Commented Dec 2, 2014 at 15:40
  • yes, I do this sorry for mistake in cde Commented Dec 2, 2014 at 15:41

1 Answer 1

2

Store the results as an array and send the entire array when the loop ends:

if ($result != NULL) {
    $items = array();

    foreach ($result as $rez) {
        $response["error"] = false;
        $response["id"] = $rez["id"];
        $response["task"] = $rez["task"];
        $response["status"] = $rez["status"];
        $response["createdAt"] = $rez["created_at"];

        $items[] = $response;
    }

    echoRespnse(200, $items);
} else {
    $response["error"] = true;
    $response["message"] = "The requested resource doesn't exists";
    echoRespnse(404, $response);
}

And echoRespnse() should json_encode the $items array.

Also, in your getAllUserTasks() function, you should use a while loop to fetch all the results from db:

if ($stmt->execute()) {
    $items = $res = array();

    $stmt->bind_result($id, $task, $status, $created_at);

    // fetch values
    while ($stmt->fetch()) {
        $res["id"] = $id;
        $res["task"] = $task;
        $res["status"] = $status;
        $res["created_at"] = $created_at;

        $items[] = $res;
    }

    $stmt->close();

    return $items;
} else {
    return NULL;
}
Sign up to request clarification or add additional context in comments.

4 Comments

ok, but now I get wrong data like this: [4] 0: { error: false id: null task: null status: null createdAt: null }- 1: { error: false id: "C" task: "C" status: "C" createdAt: "C" }- 2: { error: false id: null task: null status: null createdAt: null }- 3: { error: false id: "2" task: "2" status: "2" createdAt: "2" }
@LaraBeginer Maybe you implemented my recommendation in the wrong place ? See my updated example.
again I get just: [1] 0: { error: false id: 2 task: "Create something" status: 0 createdAt: "2014-12-01 01:58:42" } so just first data (first user task from tasks) I will update code with funcction getAllUserTasks () now
Its a good answer but peformance is worst that using get_result() function from my localhost ... but again THANKS!

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.