Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Conectar{
- function __construct(){
- }
- publuc function conectar(){
- try{
- $conexion = new PDO('mysql:host=localhost;dbname=bd','usuario','password');
- }catch(Exception $e){
- echo "Error, vuelva más tarde";
- }
- return $conexion;
- }
- }
- ?>
- class Conectar
- {
- private $conexion;
- private $credenciales;
- public function __construct()
- {
- $this->getConection();
- }
- private function getConection()
- {
- try {
- # Credenciales tomadas de un archivo protegido
- $this->credenciales = parse_ini_file("ruta/de/db.ini");
- $dsn = 'mysql:dbname=' . $this->credenciales["dbnombre"] . ';host=' . $this->credenciales["host"] . '';
- $pwd = $this->credenciales["clave"];
- $usr = $this->credenciales["usuario"];
- # Intentar la conexión
- $this->conexion = new PDO($dsn, $usr, $pwd, $options);
- }
- catch (PDOException $e) {
- }
- }
- $db=new Conectar();
Add Comment
Please, Sign In to add comment