I started working and I'm updating the company system, it currently uses this connection:
$link = mysqli_connect("0.0.0.0", "teste", "teste", "teste");
mysqli_set_charset($link, "utf8");
if (mysqli_connect_errno()) {
printf("<br> Nao foi possivel conectar. Erro: %s\n", mysqli_connect_error());
exit();
}
$database_host = "0.0.0.0";
$database_user = "teste";
$database_password = "teste";
$database_db = "teste";
$conexao = new mysqli($database_host, $database_user, $database_password, $database_db);
mysqli_set_charset($conexao, "utf8");
if ($conexao->connect_error) {
die("A conexão falhou!");
}
date_default_timezone_set("America/Sao_Paulo");
I've learned and use this type:
class ConexaoBanco extends PDO {
private static $instancia = null;
public function ConexaoBanco($dsn, $usuario, $senha) {
//Construtor da classe pai PDO
parent::__construct($dsn, $usuario, $senha);
}
public static function getInstancia() {
if (!isset(self::$instancia)) {
try {
// Cria e retorna uma nova conexão
self::$instancia = new ConexaoBanco("mysql:dbname=teste;host=0.0.0.0", "teste", "teste");
} catch (Exception $e) {
echo 'Erro ao conectar o banco de dados!';
exit();
}
}
return self::$instancia;
}
}
Which of the two is better? Why?