Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $HOST='localhost';
- $USER='root';
- $PASS='pass';
- $DATA='basedatos';
- ?>
- <?php
- include 'config.php';
- $conexion= new mysqli($HOST, $USER, $PASS,$DATA) ;
- if ($conexion->connect_error) {
- die();
- }
- ?>
- <?php
- class Conexion {
- private $servidor = "localhost";
- private $usuario = "root";
- private $contrasena = "example@";
- private $based = "ejemplo";
- private $conn;
- public function conectar(){
- $this->conn = new mysqli(
- $this->servidor,
- $this->usuario,
- $this->contrasena,
- $this->based
- );
- if ($this->conn->connect_errno) {
- echo "Fallo al contenctar a MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error;
- }
- echo $this->conn->host_info . "n";
- }
- public function desconectar(){
- self::conectar();
- $this->conn->close();
- }
- }
- $ejemplo = new Conexion();
- $ejemplo->conectar();
- ?>
- <?php
- class Conexion extends mysqli {
- public function __construct($host, $usuario, $contrasena, $bd) {
- parent::__construct($host, $usuario, $contrasena, $bd);
- if (mysqli_connect_error()) {
- die('Error de Conexión (' . mysqli_connect_errno() . ') '
- . mysqli_connect_error());
- }
- }
- }
- $ejemplo = new Conexion('localhost', 'root', 'example@', 'ejemplo');
- echo 'Éxito... ' . $bd->host_info . "n";
- $ejemplo->close();
- ?>
- <?php
- /*
- * Mysql database class - only one connection alowed
- */
- class Database {
- private $_connection;
- private static $_instance; //The single instance
- private $_host = "HOSTt";
- private $_username = "USERNAME";
- private $_password = "PASSWORd";
- private $_database = "DATABASE";
- /*
- Get an instance of the Database
- @return Instance
- */
- public static function getInstance() {
- if(!self::$_instance) { // If no instance then make one
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- // Constructor
- private function __construct() {
- $this->_connection = new mysqli($this->_host, $this->_username,
- $this->_password, $this->_database);
- // Error handling
- if(mysqli_connect_error()) {
- trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
- E_USER_ERROR);
- }
- }
- // Magic method clone is empty to prevent duplication of connection
- private function __clone() { }
- // Get mysqli connection
- public function getConnection() {
- return $this->_connection;
- }
- }
- ?>
- <?php
- $db = Database::getInstance();
- $mysqli = $db->getConnection();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement