Soy un poco novato en esto asi que perdonen la pregunta. Porque en la funcion "disconnect", al hacer un llamado a la funcion close() (de mysqli) no responde?
class DBConnection {
private $connection;
// On instance created connect to db
public function __construct() {
$this->connect();
}
private function connect() {
require_once 'db_config.php';
$this->connection = @new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
if(!$this->connection)
die('Couldnt connect to db: ' . $this->connection->connect_error);
}
private function disconnect() {
$this->connection->close();
}
public function __destruct() {
// Check if connection was established
if($this->connection != NULL) {
$this->disconnect();
}
}
}
Que quieres decir que no responde?
class DBHandler {
private $link;
private $db_name;
// On instance created connect to host
// For installation, database name is null
public function __construct($db_name = NULL) {
$this->db_name = $db_name;
$this->link = $this->connection($db_name);
}
private function connection() {
$this->link = @new mysqli(DB_HOST, DB_USER, DB_PASSWORD, $this->db_name);
if($this->link)
return $this->link;
else
die('Couldnt connect to db: ' . $this->link->connect_error);
}
private function disconnect() {
$this->link->close();
}
public function __destruct() {
// Check if connection was established
if($this->link != NULL)
$this->disconnect();
}
}
Recibo:
CitarPHP Warning: mysqli::close(): Couldn't fetch mysqli in includes/DBHandler.php on line 24
o sea en la linea:
$this->connection->close();
Es error con tu conexión o con los datos de acceso, funciona bien:
define("DB_HOST", 'localhost');
define("DB_USER", 'root');
define("DB_PASSWORD", '');
ini_set("display_errors", 1);
class DBHandler {
private $link;
private $db_name;
// On instance created connect to host
// For installation, database name is null
public function __construct($db_name = NULL) {
$this->db_name = $db_name;
$this->connection($db_name);
}
private function connection() {
$this->link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, $this->db_name);
if(!$this->link)
die('Couldnt connect to db: ' . $this->link->connect_error);
}
private function disconnect() {
$this->link->close();
}
public function __destruct() {
// Check if connection was established
if($this->link != NULL)
$this->disconnect();
}
public function get_link()
{
return $this->link;
}
}
$my_db = new DBHandler('mi_super_base_de_datos');
$my_handler = $my_db->get_link();
printf("Versión de la biblioteca cliente: %s\n", $my_handler->client_info);
exit;