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 Answers 3

3

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

same problem, must be something wrong with how im writing/accessing $connection
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.
the problem has got to be to do with the "Resource id #6" ?
0

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.

8 Comments

i'm pretty new-ish to OOP so i thought maybe i was making an error in the way i'm storing/referencing $connection ?
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 :)
my if(!self::$connection) is always returning true becuase of the Resource Id #6 - you sure $connection shouldn't be boolean?
I've added further explanation to my answer.
found the problem, see my ammended q
|
0

i found a solution, see updated question above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.