检查MySQL连接(OOP PHP)

前端之家收集整理的这篇文章主要介绍了检查MySQL连接(OOP PHP) 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码的各个部分要求我检查是否已建立mysql连接.下面我正在使用if(self :: $connection),但是self :: connection似乎总是返回“ Resource id#6”而不是boolean-我在做什么错?

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;
    }
...

谢谢!

我的解决方案:断开连接时再次使$connection为假…

static function disconnect(){
    MysqL_close(self::$connection);
    self::$connection = FALSE;
}
最佳答案
只需使用mysql_ping()方法

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
}

猜你在找的MySQL相关文章