1
I’m making a connection to bank postgres but there’s been an error I can’t determine, following the codes:
Config.php:
define('DB','sistema_postgres');
define('HOST','localhost');
define('USER','postgres');
define('PASS','123');
Database.php:
require_once 'config.php';
class Database {
private $db;
private $host;
private $user;
private $pass;
private $conn;
public function __construct(){
$this->host = HOST;
$this->db = DB;
$this->user = USER;
$this->pass = PASS;
$this->conn = new PDO("pgsql:host={$this->host};port=5432;dbname={$this->db};user={$this->user};password={$this->pass}");
}
protected function getConn(){
return $this->conn;
}
}
Turmadao.php
require_once('database.php');
class TurmaDAO extends Database implements IDAO{
private $turma;
private $db;
public function __construct($turma=null) {
if(isset($turma)){
$this->setTurma($turma);
}
parent::__construct();
$this->db = parent::getConn();
}
public function listAll() {
$stmt = $this->db->prepare('SELECT * FROM turma');
$stmt->execute();
try{
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e){
echo $e->getMessage();
die();
}
return $rs;
}
}
php class.
require_once 'TurmaDAO.php';
$t = new TurmaDAO();
$rs = $t->listAll();
var_dump($rs);
The following error message is being returned:
Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[08006] [7] FATAL: password Authentication failed for user "SYSTEM"'
Anyway, it is an error with the connection data, the strange thing is that the database user defined in my application is 'postgres' and something is changing the user to 'SYSTEM'
What may be causing this error ?
Before making the connection check which user is set on the connection
– rray
If you take a look at PDO documentation you will see that the username and password should be passed as parameters to the constructor, not within the dsn string. Try to change
$this->conn = new PDO("pgsql:host={$this->host};port=5432;dbname={$this->db};user={$this->user};password={$this->pass}");
for$this->conn = new PDO("pgsql:host={$this->host};port=5432;dbname={$this->db};", $this->user, $this->pass);
– tayllan
tayllan, important remark, corrected here, however, is not the source of the error.
– Adriano Luz
@tayllan the DSN of postgres is different from the DSN of Mysql you can pass everything in the first argument if you want, see in manual
– rray
@rray true, did not know. vlw by heads up.
– tayllan
In the __Construct database do a
print_r($this);
this before thenew PDO(...)
see what comes back and puts it there– rray
@rray made the following code change: instead of directly setting the connection data I created the methods
setters
and encapsulated them asprivate
. This solved the problem, however not yet say why the connection data was being modified.– Adriano Luz
But in print_r some value changed? came the so 'SYSTEM'?
– rray
@rray strangely no, the mistake was happening during the inheritance... maybe it’s some problem with my apache, well, I can’t say. I’ll run some tests here to find out. If you find out I’ll post a comment here
– Adriano Luz