When using prepared staments with MySQLi in a select, you first need to pass the result from the database 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 is done with bind_result ()
Change 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;
}