$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;
}
}
行$main[0]->tasks = $task;
显示错误Undefined variable: task
。在我的情况下,我的记录中没有任务,因此执行失败。但我希望它只是跳过并继续,因为我已经将它放在' if'中,它检查任务对象是否为空。
答案 0 :(得分:2)
做类似
的事情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;
}
}