Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. <?php
  2. $HOST='localhost';
  3. $USER='root';
  4. $PASS='pass';
  5. $DATA='basedatos';
  6. ?>
  7.  
  8. <?php
  9. include 'config.php';
  10. $conexion= new mysqli($HOST, $USER, $PASS,$DATA) ;
  11.  
  12. if ($conexion->connect_error) {
  13. die();
  14. }
  15. ?>
  16.  
  17. <?php
  18.  
  19. class Conexion {
  20. private $servidor = "localhost";
  21. private $usuario = "root";
  22. private $contrasena = "example@";
  23. private $based = "ejemplo";
  24. private $conn;
  25.  
  26. public function conectar(){
  27.  
  28. $this->conn = new mysqli(
  29. $this->servidor,
  30. $this->usuario,
  31. $this->contrasena,
  32. $this->based
  33. );
  34.  
  35. if ($this->conn->connect_errno) {
  36. echo "Fallo al contenctar a MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error;
  37. }
  38.  
  39. echo $this->conn->host_info . "n";
  40.  
  41. }
  42.  
  43. public function desconectar(){
  44.  
  45. self::conectar();
  46.  
  47. $this->conn->close();
  48.  
  49. }
  50.  
  51. }
  52. $ejemplo = new Conexion();
  53. $ejemplo->conectar();
  54. ?>
  55.  
  56. <?php
  57. class Conexion extends mysqli {
  58. public function __construct($host, $usuario, $contrasena, $bd) {
  59. parent::__construct($host, $usuario, $contrasena, $bd);
  60. if (mysqli_connect_error()) {
  61. die('Error de Conexión (' . mysqli_connect_errno() . ') '
  62. . mysqli_connect_error());
  63. }
  64. }
  65. }
  66. $ejemplo = new Conexion('localhost', 'root', 'example@', 'ejemplo');
  67.  
  68. echo 'Éxito... ' . $bd->host_info . "n";
  69. $ejemplo->close();
  70. ?>
  71.  
  72. <?php
  73. /*
  74. * Mysql database class - only one connection alowed
  75. */
  76. class Database {
  77. private $_connection;
  78. private static $_instance; //The single instance
  79. private $_host = "HOSTt";
  80. private $_username = "USERNAME";
  81. private $_password = "PASSWORd";
  82. private $_database = "DATABASE";
  83. /*
  84. Get an instance of the Database
  85. @return Instance
  86. */
  87. public static function getInstance() {
  88. if(!self::$_instance) { // If no instance then make one
  89. self::$_instance = new self();
  90. }
  91. return self::$_instance;
  92. }
  93. // Constructor
  94. private function __construct() {
  95. $this->_connection = new mysqli($this->_host, $this->_username,
  96. $this->_password, $this->_database);
  97.  
  98. // Error handling
  99. if(mysqli_connect_error()) {
  100. trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
  101. E_USER_ERROR);
  102. }
  103. }
  104. // Magic method clone is empty to prevent duplication of connection
  105. private function __clone() { }
  106. // Get mysqli connection
  107. public function getConnection() {
  108. return $this->_connection;
  109. }
  110. }
  111. ?>
  112.  
  113. <?php
  114. $db = Database::getInstance();
  115. $mysqli = $db->getConnection();
  116. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement