Ejemplo del error:
mysqli::close(): Couldn't fetch mysqli in
class Model
{
private $db_host ;
private $db_user ;
private $db_pass ;
private $db_name ;
private $db_charset;
private $connection;
private static $instance;
public function __construct(){
try {
$this->setConnetion();
$this->runConnection();
} catch (ExceptionSicatsem $e) {
echo $e::__toString();
}
}
private function setConnetion(){
require 'app/config/datosDeConexion.php';
$this->db_host = $datos_conexion['db_host'];
$this->db_user = $datos_conexion['db_user'];
$this->db_pass = $datos_conexion['db_pass'];
$this->db_name = $datos_conexion['db_name'];
$this->db_charset = $datos_conexion['db_charset'];
}
private function runConnection(){
$this->connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
if(!$this->connection){
throw new ExceptionSicatsem("Error al intentar conectar con la base de datos");
}else {
$this->connection->set_charset($this->db_charset);
}
}
public static function getInstance(){
if(static::$instance === null){
static::$instance = new Model();
}
return static::$instance;
}
public function query($sql){
if($this->connection->connect_errno){
die('Error de Conexión (' . $this->connection->error . ') ');
}else {
return $this->connection->query($sql);
}
}
public function rowsQuery($result){
if(isset($result) && is_resource($result)){
return $result->num_rows();
}
}
public function rowsOpe(){
if($this->connection->connect_errno){
die('Error de Conexión (' . $this->connection->error . ') ');
}else {
return $this->connection->affected_rows;
}
}
public function liberar($result){
if(isset($result) && is_resource($result)){
$result->free_result();
}
}
public function listNum($query){
if(isset($query)){
return $query->fetch_array(MYSQLI_NUM);
}
}
public function listAssoc($query){
if(isset($query)){
return $query->fetch_array(MYSQLI_ASSOC);
}
}
public function listNumAssoc($query){
if(isset($query)){
return $query->fetch_array(MYSQLI_BOTH);
}
}
public function close(){
if(!$this->connection) {
die('Error de Conexión (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
} else{
$this->connection->close();
}
}
}
getInstance()
(edita tu respuesta y pon un ejemplo) y ya tendré todo para reproducir el problema. Teóricamente te devuelvenull
porque haces$this->connection
, que crea una propiedad de clase en la que guardar la conexión en vez de modificar el valor estático (si no pones elthis
). Modificalo y me dices.