To lazy to do a simple search? http://php.net/manual/en/book.pdo.php http://php.net/manual/en/pdo.construct.php
Can you explain me: Case 1 ==== <?php class ConexionDB extends PDO { public function __construct () { try { parent:: __construct('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'keys'); parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $err) { die ('There is a Error in DB'); } } function __destruct(){ } } ?> PHP: In other file.php $query = "SELECT * FROM prueba"; $BD = new ConexionDB(); $recordSet = $BD->prepare($query); $recordSet->execute(); PHP: Case 2 ==== class connect_pdo { protected $dbh; public function __construct() { try { $con = new PDO('mysql:host='.$db_host.'; dbname='.$db_name, $db_user, $user_pw); $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $con->exec("SET CHARACTER SET utf8"); } catch (PDOException $err) { echo "harmless error message if the connection fails"; $err->getMessage() . "<br/>"; file_put_contents('PDOErrors.txt',$err, FILE_APPEND); die(); } } public function dbh() { return $this->dbh; } } PHP: In other file.php $con = new connect_pdo(); $con = $con->dbh(); PHP: What is the diference between Case 1 and Case 2, and what is the more safe connection class?