Guest User

Untitled

a guest
Jan 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. <?php
  2.  
  3. class Conectar{
  4.  
  5. function __construct(){
  6.  
  7. }
  8. publuc function conectar(){
  9. try{
  10.  
  11. $conexion = new PDO('mysql:host=localhost;dbname=bd','usuario','password');
  12. }catch(Exception $e){
  13. echo "Error, vuelva más tarde";
  14. }
  15. return $conexion;
  16. }
  17. }
  18. ?>
  19.  
  20. class Conectar
  21. {
  22. private $conexion;
  23. private $credenciales;
  24.  
  25.  
  26. public function __construct()
  27. {
  28. $this->getConection();
  29.  
  30. }
  31.  
  32. private function getConection()
  33. {
  34.  
  35. try {
  36. # Credenciales tomadas de un archivo protegido
  37. $this->credenciales = parse_ini_file("ruta/de/db.ini");
  38. $dsn = 'mysql:dbname=' . $this->credenciales["dbnombre"] . ';host=' . $this->credenciales["host"] . '';
  39. $pwd = $this->credenciales["clave"];
  40. $usr = $this->credenciales["usuario"];
  41.  
  42. # Intentar la conexión
  43. $this->conexion = new PDO($dsn, $usr, $pwd, $options);
  44.  
  45. }
  46. catch (PDOException $e) {
  47.  
  48. }
  49. }
  50.  
  51. $db=new Conectar();
Add Comment
Please, Sign In to add comment