1
<?php
class Usuario {
private $login;
private $senha;
private $admin;
//variaveis internas
private $bd; //conexão com o banco
private $tabela; //nome da tabela
public function __construct() {
$this->bd = new BD();
$this->tabela = "usuario";
}
public function listar($complemento = "") {
$sql = "SELECT * FROM $this->tabela ".
$complemento;
$resultado = pg_query($sql);
$retorno = NULL;
//percorre os registros
while ($reg = pg_fetch_assoc($resultado)) {
//transforma em objetos categoria
$obj = new Usuario(); // mudar
$obj->login = $reg["login"];
$obj->senha = $reg["senha"];
$obj->admin = $reg["admin"];
//adiciona a variavel de retorno
$retorno[] = $obj;
}
return $retorno;
}
}
?>
In that part:
while ($reg = pg_fetch_assoc($resultado)) {
//transforma em objetos categoria
$obj = new Usuario(); // mudar
$obj->login = $reg["login"];
$obj->senha = $reg["senha"];
$obj->admin = $reg["admin"];
//adiciona a variavel de retorno
$retorno[] = $obj;
}
When creating an object of type Usuario
within the while
, Won’t it be connecting all the time in the bank? no construct
it is doing starting the connection with the bank, is not it? it works, but the correct is not to connect the bank only once and then go through the records?
public function __construct() {
$this->bd = new BD();
$this->tabela = "usuario";
}
BD-class:
class BD {
public function __construct() {
pg_connect("host=localhost user=postgres
password=123 dbname=ss port=5432")
or die("Erro ao conectar ao servidor");
}
}
edited and updated...
– Jose Maximilian
You have 2 interesting questions that might be helpful to your case: question 1, question 2
– Papa Charlie