0
$stmt = $db->prepare("SELECT * FROM task where uId = ?");

$stmt->bind_param('s',$userId);

if($stmt->execute()){
    $user = $stmt->get_result();
    while ($obj = $user->fetch_object()) {
         $task[] = $obj;
    }

    if(count((array)$task)){
        $main[0]->tasks = $task;
    }
}

line $main[0]->tasks = $task; shows an error of Undefined variable: task. In my case there is no task in my record so the execution failed. But I expect it just skip and continue because I already put it within the 'if', which check whether the task object is empty.

3
  • 1
    why you are using cast at all? Commented May 9, 2014 at 5:47
  • how many rows this query supposed to return? if only one - why loops and arrays at all? Commented May 9, 2014 at 5:48
  • 1
    It's already an array. You just need to define it before your while loop. $task = array();. Commented May 9, 2014 at 5:48

1 Answer 1

2

do something like

if($stmt->execute()){
    $task = array();
    $user = $stmt->get_result();
    while ($obj = $user->fetch_object()) {
         $task[] = $obj;
    }

    if(count($task)){ //or if($task){
        $main[0]->tasks = $task;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

so I forgot to init the $task array?

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.