I can’t connect to my database, please help.
can you provide the file name and/or contents (excluding private login details), and I can see how you setup the config/other file name? Because there may be a typo, incorrect details, etc, and the connection must be in a php code.
Erro SQLSTATE[HY000] [2002] Connection refused
Fatal error : Uncaught Error: Call to a member function prepare() on null in /home/vol15_6/epizy.com/epiz_24233891/htdocs/SIGA/classes/ConfiguracaoInstituicao.class.php:136 Stack trace: #0 /home/vol15_6/epizy.com/epiz_24233891/htdocs/SIGA/index.php(12): ConfiguracaoInstituicao->dados_instituicao() #1 {main} thrown in /home/vol15_6/epizy.com/epiz_24233891/htdocs/SIGA/classes/ConfiguracaoInstituicao.class.php on line 136
function:
public function dados_instituicao(){
try {
$cst = $this->con->conectar()->prepare("SELECT * FROM `tb_instituicao`");
$cst->execute();
return $cst->fetch();
} catch (PDOException $e) {
return 'erro' . $e->getMessage();
}
}
database class:
class Conexao {
#ATRIBUTOS PRIVADOS
private $servidor;
private $porta;
private $usuario;
private $senha;
private static $pdo;
// #METODO CONSTRUTOR
public function __construct(){
$this->servidor = "sql310.epizy.com";
$this->porta = 3306; // Adicione a porta padrão aqui
$this->usuario = "epiz_24233";
$this->senha = "SMTusODpWGjYAp";
}
#METODO PARA CONECTAR
public function conectar(){
try {
if(is_null(self::$pdo)){
self::$pdo = new PDO("mysql:host=".$this->servidor.";port=".$this->porta.";dbname=epiz_24233891_siga;charset=utf8", $this->usuario, $this->senha);
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo "Conectado com sucesso!";
}
return self::$pdo;
} catch (PDOException $e) {
echo "Erro ".$e->getMessage();
}
}
}
Sensitive details exposed
Please reset or change your account password through the client area.
Definitely wrong
I changed it to $this->usuario = “epiz_24233891”; the correct data but it still doesn’t work, it still has the same error.
I just reset your hosting account password.
Please note that this is a public forum, anything you post here can be seen by anyone. So make sure to not share any confidential information here, notably, and especially, any passwords.
Feel free to change the password again, but don’t use the password you used before, because it is now compromised.
Thanks for the alert. But the error still persists.
I dove into the issue, because it was a weird head scratcher. But I found the issue.
If you var_dump
the contents of the $this->con
variable within the ConfiguracaoInstituicao
class, you would expect to see the database variables defined in SIGA/classes/Conexao.class.php
. But that’s not what I saw: I saw database host 127.0.0.1
and username root
.
So why is this? Because your project has two classes with the name Conexao
. One in the SIGA
folder directly, and another one in the classes
directory. And the one in the SIGA
folder has the wrong credentials.
Now I assume that the one in the SIGA
folder gets loaded first. So when in the ConfiguracaoInstituicao
you try to load the other Conexao
class, it doesn’t do anything because another Conexao
class was already loaded.
So to fix this, you could update the database credentials in the SIGA/Conexao.class.php
file, but I think you’ll instead just one to have one copy of this class in your codebase, and not two.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.