Check MySQL Connection (OOP PHP)

I have different parts of my code that require me to check if a mysql connection is established. Below I use if(self::$connection), but self::connectionit always seems to return “Resource Identifier # 6” and not 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;
    }
...

thank!


my solution : disconnect $ connection false again when disconnected ...

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

I found a solution, see the updated question above.

0
source

Just use the mysql_ping () method

, . , . , , , .

TRUE, MySQL , FALSE.

static function isActive() {
   return mysql_ping($connection);//returns boolean
}
+3

mysql_connect , . . , . - ?

:

mysql_connect() MySQL FALSE . , $connection null ( false), . , . , $connection " MySQL", true. " MySQL", - " №6". , .

0

Source: https://habr.com/ru/post/1758402/


All Articles