-1
class mysql{

    function __construct(){
        $this->connect();
    }

    function __destruct(){
        $mysqli = $this->connect();
        $mysqli->close();
    }

    public function connect(){

        static $mysqli;

        require_once(ROOT_PATH.'/config/db_conf.php');

        @$mysqli = new mysqli($sql_login['host'], $sql_login['user'], $sql_login['password'] , $sql_login['database'], $sql_login['port']);

        if(!$mysqli){
            throw new Exception("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
        }

        return $mysqli;
    }
}

When i trying to connect to mysql - everything is fine, but after it i am getting Warning: mysqli::close(): Couldn't fetch mysqli in...

Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
user1564141
  • 5,911
  • 5
  • 21
  • 18
  • 2
    why are you calling connect again in the destructor ? – Oussama Jilal Aug 06 '12 at 11:59
  • 2
    Please remove the `@` character. Also you do not need to create a mysql class, MySQLI is already a "mysql" class. – hakre Aug 06 '12 at 12:00
  • @Yazmat i am new in oop, someone from stackoverlofw once showed it to me. – user1564141 Aug 06 '12 at 12:05
  • @hakre Agreed, although I have seen some nice frameworks that *extend* MySQLi to provide some sugar methods to make prepared statements a bit more user-friendly and PDO-like. Although I still don't get why you wouldn't just *use PDO...* – DaveRandom Aug 06 '12 at 12:06
  • Please do not ask questions about code that is not your own. You can only evolve if you start write code your own. Just copying something over because someone told you "that is it" will bring you into many troubles not worth the work and pretty frustrating. Sure it's hard to start, but it's worth. – hakre Aug 06 '12 at 12:20
  • This might give you some idea how you can reduce the code to a single line: http://stackoverflow.com/questions/10919277/how-to-successfully-rewrite-old-mysql-php-code-with-deprecated-mysql-functions - PDO is a killer here. – hakre Aug 06 '12 at 12:33

1 Answers1

3

To answer the question directly:

  1. Move the "static $mysqli;" to be a class variable "var $mysqli;".
  2. Move the connection string into the construct. (Why have a function that only calls another function in that manner? Unless you're going to connect a second time = unlikely unless you're messing around with multiple connections?).
  3. When you connect, use the class variable (not the static. Lose the @ as hakre says.)
  4. When you destruct, don't recreate a new connection, just close the one you have (as Yazmat says).

BUT: do you have plans for this class? mysqli itself is a class and probably gives you all you need at this stage. I'd start by using the base class first before enhancing it, then enhance it when you've more of an idea where you need extra functionality. Keep it simple at first :)

Robbie
  • 17,605
  • 4
  • 35
  • 72
  • Edit: just noticed you're discussing the benefits of reclassing mysqli in the comments above. PDO is another option (the one I use) but again, keep it simple and don't enhance until you know the shortcomings (such as building nice error traps / reporting etc). – Robbie Aug 06 '12 at 12:14