I have doubts if this would be the correct way to make a connection using the class PDO

1

I'm wondering if this form is actually correct to make a connection between php and mysql with the class PDO. The question is this: Every time I have a file that uses this connection it will create a new connection

Class PDOUtil {

public static final function conectar() {
    try {
        $conexao = new PDO('mysql:host=localhost;dbname=site_local', 'root', '');
    } catch (PDOException $ex) {
        echo $ex->getMessage();

        if ($ex->getCode() == "2002") {
            echo 'Oi infelizmente seu Host nao foi encontrado, verifique isso com o web-master.';
        }
        if ($ex->getCode() == "1049") {
            echo 'Oi infelizmente seu banco nao foi encontrado, verifique isso com o web-master.';
        }
        if ($ex->getCode() == "1044") {
            echo 'Oi infelizmente seu usuario nao foi encontrado, verifique isso com o web-master.';
        }
        if ($ex->getCode() == "1045") {
            echo 'Oi infelizmente sua senha nao foi encontrada, verifique isso com o web-master.';
        }
    }
    return $conexao;
}

}

An example usage

include '../config/PDOUtil.php';
$con = new PDOUtil();
$con->conectar();
    
asked by anonymous 08.12.2014 / 23:36

2 answers

3

Your script is right, but you will be creating a new connection every time you request it. Try this instead, using the standard Singleton ;

Class PDOUtil {
private static $oInstance;

public static function getConexao() {

  if (!(self::$oInstance instanceof PDO)) {
    try {
      $conexao = new PDO('mysql:host=localhost;dbname=site_local', 'root', '');
    } catch (PDOException $ex) {
      echo $ex->getMessage();

      if ($ex->getCode() == "2002") {
        echo 'Oi infelizmente seu Host nao foi encontrado, verifique isso com o web-master.';
      }

      if ($ex->getCode() == "1049") {
        echo 'Oi infelizmente seu banco nao foi encontrado, verifique isso com o web-master.';
      }

      if ($ex->getCode() == "1044") {
        echo 'Oi infelizmente seu usuario nao foi encontrado, verifique isso com o web-master.';
      }

      if ($ex->getCode() == "1045") {
        echo 'Oi infelizmente sua senha nao foi encontrada, verifique isso com o web-master.';
      }
      exit;
    }

  }
  return self::$oInstance;
}

/* TORNA O CONSTRUTOR PRIVADO PARA PROIBIR DE INSTANCIA ESTA CLASSE */
private function __construct() {}

}

To use now you can always do it this way:

$conexao = PDOUtil::getConexao();

In this way only a connection will exist in your application and you can get it easily;

    
09.12.2014 / 11:31
1

In PDO you just need a connection to the entire "existence" of the script, just as you would with mysql _ *.

$conn = new PDO(
    'mysql:host=localhost;dbname=example-pdo', 'andre', '123456',
    array(
        PDO::ATTR_PERSISTENT => true
    )
);

In the example above, we configured our connection as persistent. A persistent connection is not closed at the end of the script, but is cached and reused when another script requests a connection using the same credentials.

Your form is correct!

    
09.12.2014 / 00:16