-1
I’ve got the spread connection:
<?php
namespace classes\conexao;
use PDO;
class Conexao {
private static $instancia;
private static $host = "localhost";
private static $db = "mvc_crud";
private static $user = "mvc_crud";
private static $password = "mvc_crud";
private function __construct (){}
private static function obtemConexao() {
if ( !isset( self::$instancia ) ) {
try {
self::$instancia = new \PDO ('mysql:host=' . self::$host . ';dbname=' . self::$db , self::$user, self::$password);
self::$instancia->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
self::$instancia->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ );
} catch ( PDOException $e ) {
throw new PDOException( "Reveja suas credênciais de conexão ao banco de dados!<br />Contate o administrador!" );
return;
}
return self::$instancia;
}
}
public static function abreConexao () { return self::obtemConexao(); }
}
?> I can’t instantiate it twice
I know it’s not recommended this, but what if you test by putting
$this->pdo = Conexao::abreConexao();
inside the include function, to see if you’re getting the connection?– Rodrigo Tognin
Your class
Conexao
is wrong.– Woss
@Rodrigotognin: Connection::abreConexao(); gives NULL.
– Carlos Rocha
@Andersoncarloswoss, I think you are not wrong otherwise the other class would not work. It is not no?
– Carlos Rocha
No. That’s just the mistake: it only works on the first call. If you [Edit] the question, add its code (and remove everything you have now), I can explain/answer.
– Woss
@Andersoncarloswoss, I understood, added at the end of the question the connection class
– Carlos Rocha
I edited the question by removing all the noise. That’s making a minimum example.
– Woss
PDO or mysqli? In the connection class you are using mysqli to connect...
– Rodrigo Tognin
You are calling the method
abreConexao
as if it were static but it is not– Costamilam
Please forgive me? I put the wrong class in the question. Now I got the class right there. This is where the error occurs. Can you look at it one more time? @Andersoncarloswoss,
– Carlos Rocha
Can do the table test of function
obtemConexao
?– Woss
@Andersoncarloswoss. I put the vision class (View) at the end of the question. Note that the Pdo class is called a few times and none of them gives this.
– Carlos Rocha