加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

检查MySQL连接(OOP PHP)

发布时间:2021-01-27 10:34:03 所属栏目:MySql教程 来源:网络整理
导读:我代码的各个部分要求我检查是否已建立mysql连接.下面我正在使用if(self :: $connection),但是self :: connection似乎总是返回“ Resource id#6”而不是boolean-我在做什么错? class mysql_lib{ static $connection; static function connect($user = FAL

我代码的各个部分要求我检查是否已建立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
}

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读