postgresql php connection problem with objects

0

Hi, I'm having trouble connecting to the DB with postgres using PHP I attach the php code of the class connection, when I create an object connections and I call the method to connect this does not return anything to me, is there any way to see the error as mysql_error ??

    class Conexiones
    { /*ATRIBUTOS*/
      private $servidor='localhost';
      private $bd='baseclientes';
      private $user='postgres';
      private $password='hom';
      private $postgres= 0;

      /** METODOS */
      public function conectar ()
      {
       $pgConnectionString = "host='$this->servidor' port=5432 dbname='$this->bd' user='$this->user' password='$this->password'";

if ($this->postgres= pg_connect($pgConnectionString))
     {return 0;}// todo ok
else {return 1;}
} /* FIN METODO CONEXION*/

} /*FIN CLASE CONEXIONES*/

ps: I never worked with postgres, it's likely that I'm missing somewhere, greetings

    
asked by fer 11.10.2017 в 17:20
source

1 answer

0

Possibly modify the class that you already have in this way you can indicate when instantiating the class if the connection was made, additionally for practical issues I do not recommend that within a class to make a database connection contain the credentials .

<?php

class Conexiones
{

    private $connexion = NULL;

    public function _construct ($host, $dbname, $username, $password)
    {
        $conn_string = "host=$this->servidor port=5432 dbname=$this->db user=$this->user password=$this->password";
        $this->connexion = pg_connect($conn_string) or die('Conexión fallida');
        return $this->connexion;

    }
}


$credentials = [
    'host' => 'localhost',
    'dbname' => 'baseclientes',
    'username' => 'postgres',
    'password' => 'hom'
];

$pgconn = new Conexiones($credentials['host'], $credentials['dbname'], $credentials['username'], $credentials['password']);

There is better documentation on the official site of PHP - function pg_connect

    
answered by 11.10.2017 в 18:52