Which database connection is best? [duplicate]

1

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?

    
asked by anonymous 23.12.2017 / 21:50

1 answer

1

There is no better or worse, in this case, because you use the mysql database. If you used another database, such as PostgreSQL, you would need to use the PDO. Because PDO supports more banks, whereas mysqli supports only the mysql database.

There is a debate about which is better or worse. ( here for example )

But, in my view, both have a lot of security and performance. If you are used to using the PDO, go ahead and continue.

    
23.12.2017 / 22:35