The correct one is not to connect the bank only once and then go through the records?

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 this 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 while , will it not stay connected all the time at the bank? no construct he's doing it by starting the connection to the bank, right? this works, but the correct one 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");
    }
}
    
asked by anonymous 11.03.2015 / 05:59

1 answer

2

Yes, the correct thing would be to establish the connection once. Just do not put the connection in the constructor and do the part. Or as said @chambelix, use the variable bd as static . And in that case it would not be necessary to instantiate the class to use bd.

Now this is working? Should not. Unless you have already opened the connection before using this class.

The code is:

<?php
class Usuario {

    private $login;
    private $senha;
    private $admin;

    //variáveis internas  
    //private $bd;           // sem static

    private static $bd;                          
    private $tabela; //nome da tabela

    public function __construct() {
        $this->tabela = "usuario";
        if(!isset(self::$db)) {       //sem static retirar
            self::$db = new DB();     //estas linhas
        }
    }

    //sem static
    //private function connect() {
    //    
    //    $this->bd = new BD();
    //    return $this->bd;
    //}

    public function listar($complemento = "") {
        $sql = "SELECT * FROM $this->tabela ".
                $complemento;   

        //sem static
        //$connection = $this->connect();                       
        //$resultado  = pg_query($connection, $sql);

        $resultado  = pg_query(self::$db, $sql);
        $retorno    = NULL;
        //percorre os registros
        while ($reg = pg_fetch_assoc($resultado)) {
            //transforma em objetos categoria
            $obj = new Usuario();                 
            $obj->login = $reg["login"];
            $obj->senha = $reg["senha"];
            $obj->admin = $reg["admin"];
            //adiciona a variavel de retorno
            $retorno[] = $obj;
        }
        return $retorno;
    }
}
?>

Notice that I used the DB connection to make the query as recommended in documentation :

  

Note: connection is an optional parameter for pg_query (). If   connection is not defined, the default connection will be used. The connection   default is the last connection made by pg_connect () or pg_pconnect ().   Although connection can be omitted, this is not recommended since   can be a cause of hard-to-find errors in your script.

    
11.03.2015 / 10:12