I'm developing a CRUD Application with PHP OO and I have a problem with this listing function in postgres, is showing an error in my php page HTML:
( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\CRUD-Postgres\pessoa.php on line 47
Function:
function busca() {
require_once ('db_conecta.php');
$query = ("SELECT * FROM pessoas ");
$stmt = pg_query($con, $query);
$resultados = pg_fetch_array($stmt);
$pessoas = array();
foreach ($resultados as $item) {
$pessoa = new Pessoa();
$pessoa -> setId($item['id']);
$pessoa -> setNome($item['nome']);
$pessoa -> setFone($item['fone']);
$pessoa -> setEmail($item['email']);
array_push($pessoas, $pessoa);
}
return $pessoas;
}
pg_fetch_Array
will return false
if there are no rows to retrieve, try outputting the value before the foreach
.
It also only returns a single row. Perhaps you intended to use pg_fetch_all
.