tags:

views:

50

answers:

3

I have various parts of my code that require me to check if a mysql connection is already established. Below i'm using if(self::$connection), but self::connection seems to always return "Resource id #6" rather than boolean - what am i doing wrong?

class mysql_lib{
    static $connection;     

    static function connect($user = FALSE){     
        if(!$user){
            $user = 'mr_update';
        }
        $password = 'some_password';
        self::$connection = mysql_connect('localhost', $user, $password, TRUE);
        mysql_select_db('codlife_headfirst2');      
    }

    static function disconnect(){
        mysql_close(self::$connection);
    }

    static function mres($str){
        if(!self::$connection){
            self::connect('mres');
            $str = mysql_real_escape_string($str);
            mysql_close(self::$connection); 
        }
        else{
            $str = mysql_real_escape_string($str);
        }
        return $str;
    }
...

thanks!


my solution : make $connection false again on disconnection...

static function disconnect(){
    mysql_close(self::$connection);
    self::$connection = FALSE;
}
+3  A: 

Just use the mysql_ping() method

Checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted. This function can be used by scripts that remain idle for a long while, to check whether or not the server has closed the connection and reconnect if necessary.

Returns TRUE if the connection to the server MySQL server is working, otherwise FALSE.

static function isActive() {
   return mysql_ping($connection);//returns boolean
}
Pentium10
same problem, must be something wrong with how im writing/accessing $connection
Haroldo
Note that the automatic reconnection will be done with no arguments, so it will not, in the OP's case, connect to the right database if it manages to connect at all.
Scott Saunders
the problem has got to be to do with the "Resource id #6" ?
Haroldo
A: 

mysql_connect will return a resource, so that's correct. Don't let that throw you. It looks like everything should work fine. Is there something else not working?

More information:

mysql_connect() returns a MySQL link identifier on success or FALSE on failure. So your $connection variable will be null (which evaluates to false) if no connection attempt has been made. It will be false if a connection attempt was made and failed. If a connection was successfully made, $connection will be a 'MySQL link identifier' which will evaluate to true. If you try to echo or print a 'MySQL link identifier', it will print something like "Resource ID #6". That's what it's supposed to do.

Scott Saunders
i'm pretty new-ish to OOP so i thought maybe i was making an error in the way i'm storing/referencing $connection ?
Haroldo
Well, it's a little odd that everything is a static method, but not really a problem. Your code looks like it should work. Try to use it and see - if there's a problem, tell us. But the 'Resource id #8' is not a problem - that's expected. You can't print out a mysql connection :)
Scott Saunders
my if(!self::$connection) is always returning true becuase of the Resource Id #6 - you sure $connection shouldn't be boolean?
Haroldo
I've added further explanation to my answer.
Scott Saunders
found the problem, see my ammended q
Haroldo
Good catch! I will point out, though, that the life of a PHP script is usually very short, so there is little reason to disconnect until the script is over. And PHP will automatically disconnect when the script ends. No PHP code (in a normal script), including static stuff, will remain active between requests. That won't make a difference to this problem, but may be something to keep in mind as you're coding and learning OOP.
Scott Saunders
thanks scott, the reason for disconnecting is for security - different users with different privilages. ie. the script may hit an if($logged_id) and then may need to upgrade the connection to one with DELETE abilities... does this sound sensible?
Haroldo
Gotcha. I don't do it that way, but it doesn't sound crazy :)
Scott Saunders
cool, thanks for your help sir
Haroldo
A: 

i found a solution, see updated question above.

Haroldo