加入收藏 | 设为首页 | 会员中心 | 我要投稿 51站长网 (https://www.51zhanzhang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
站内搜索:
当前位置: 首页 > 站长学院 > MySql教程 > 正文

检查MySQL连接(OOP PHP)

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

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

(编辑:ASP站长)

【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

    相关内容
    未处理完善
      无相关信息
    未处理完善