When using Prepared staments with Mysqli in a select, you first need to pass the database result to php, this is done with the method/function
get_result() the great advantage of it is that you do not need to specify the columns individually as it is done with bind_result()
Changes your method to:
$query->execute();
$result = $query->get_result();
return $result->fetch_all();
}
Or if you want to do it manually.
$query->execute();
$result = $query->get_result();
$lista = array();
while($item = $result->fetch_assoc()){
$lista[] = $item;
}
return $lista;
}
After that you can get the result with fetch()
or variant in a foreach.
Example with bind_result
public function getById($id) {
$query = $this->db->prepare("SELECT nome, idade, profissao, aniversario
FROM pessoas WHERE id = ?");
$query->bind_param('i', $id);
$query->execute();
$query->bind_result($nome, $idade, $profissao, $aniversario);
$lista = array();
$i = 0;
while($query->fetch()){
$lista[$i]['nome'] = $nome;
$lista[$i]['idade'] = $idade;
$lista[$i]['profissao'] = $profissao;
$lista[$i]['aniversario'] = $aniversario;
$i++;
}
return $lista;
}
Simply use
return $query->fetch_object();
orreturn $query->fetch_assoc();
would not return what you want?– Thyago ThySofT
It returns this: Call to Undefined method mysqli_stmt::fetch_object()
– Thiago