Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: Geek7 en 10 Junio 2014, 14:10 pm



Título: conectar a base de datos con objetos
Publicado por: Geek7 en 10 Junio 2014, 14:10 pm
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?
Código
  1. class DBConnection {
  2.  
  3.    private $connection;
  4.  
  5.    // On instance created connect to db
  6.    public function __construct() {
  7.        $this->connect();
  8.    }
  9.  
  10.    private function connect() {
  11.        require_once 'db_config.php';
  12.        $this->connection = @new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
  13.  
  14.        if(!$this->connection)
  15.            die('Couldnt connect to db: ' . $this->connection->connect_error);
  16.    }
  17.  
  18.    private function disconnect() {
  19.        $this->connection->close();
  20.    }
  21.  
  22.    public function __destruct() {
  23.        // Check if connection was established
  24.  
  25.        if($this->connection != NULL) {
  26.            $this->disconnect();
  27.        }
  28.    }
  29.  
  30. }


Título: Re: conectar a base de datos con objetos
Publicado por: Shell Root en 10 Junio 2014, 14:35 pm
Que quieres decir que no responde?


Título: Re: conectar a base de datos con objetos
Publicado por: Geek7 en 10 Junio 2014, 14:53 pm
Código
  1. class DBHandler {
  2.  
  3.    private $link;
  4.    private $db_name;
  5.  
  6.    // On instance created connect to host
  7.    // For installation, database name is null
  8.    public function __construct($db_name = NULL) {
  9.       $this->db_name = $db_name;
  10.       $this->link = $this->connection($db_name);
  11.    }
  12.  
  13.  
  14.    private function connection() {
  15.        $this->link = @new mysqli(DB_HOST, DB_USER, DB_PASSWORD, $this->db_name);
  16.  
  17.        if($this->link)
  18.            return $this->link;
  19.        else
  20.            die('Couldnt connect to db: ' . $this->link->connect_error);
  21.    }
  22.  
  23.    private function disconnect() {
  24.        $this->link->close();
  25.    }
  26.  
  27.    public function __destruct() {
  28.        // Check if connection was established
  29.  
  30.        if($this->link != NULL)
  31.            $this->disconnect();
  32.    }
  33.  
  34. }

Recibo:
Citar
PHP Warning:  mysqli::close(): Couldn't fetch mysqli in includes/DBHandler.php on line 24

o sea en la linea:
Código
  1. $this->connection->close();


Título: Re: conectar a base de datos con objetos
Publicado por: JorgeEMX en 10 Junio 2014, 18:22 pm
Es error con tu conexión o con los datos de acceso, funciona bien:

Código
  1. define("DB_HOST", 'localhost');
  2. define("DB_USER", 'root');
  3. define("DB_PASSWORD", '');
  4. ini_set("display_errors", 1);
  5.  
  6. class DBHandler {
  7.  
  8. private $link;
  9. private $db_name;
  10.  
  11. // On instance created connect to host
  12. // For installation, database name is null
  13. public function __construct($db_name = NULL) {
  14.   $this->db_name = $db_name;
  15.   $this->connection($db_name);
  16. }
  17.  
  18.  
  19. private function connection() {
  20. $this->link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, $this->db_name);
  21.  
  22. if(!$this->link)
  23. die('Couldnt connect to db: ' . $this->link->connect_error);
  24. }
  25.  
  26. private function disconnect() {
  27. $this->link->close();
  28. }
  29.  
  30. public function __destruct() {
  31. // Check if connection was established
  32. if($this->link != NULL)
  33. $this->disconnect();
  34. }
  35.  
  36. public function get_link()
  37. {
  38. return $this->link;
  39. }
  40.  
  41. }
  42.  
  43. $my_db = new DBHandler('mi_super_base_de_datos');
  44. $my_handler = $my_db->get_link();
  45. printf("Versión de la biblioteca cliente: %s\n", $my_handler->client_info);
  46.