简体   繁体   English

检查MySQL连接(OOP PHP)

[英]check for MySQL connection (OOP PHP)

I have various parts of my code that require me to check if a mysql connection is already established. 我有我的代码的各个部分,要求我检查是否已建立mysql连接。 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? 下面我使用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;
    }
...

thanks! 谢谢!


my solution : make $connection false again on disconnection... 我的解决方案 :断开连接时再次使$ connection为false

static function disconnect(){
    mysql_close(self::$connection);
    self::$connection = FALSE;
}

Just use the mysql_ping() method 只需使用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. 如果与服务器MySQL服务器的连接正在工作,则返回TRUE,否则返回FALSE。

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

mysql_connect will return a resource, so that's correct. mysql_connect将返回一个资源,所以这是正确的。 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. mysql_connect()在成功时返回MySQL链接标识符,如果失败则返回FALSE。 So your $connection variable will be null (which evaluates to false) if no connection attempt has been made. 因此,如果没有进行任何连接尝试,您的$ connection变量将为null(其计算结果为false)。 It will be false if a connection attempt was made and failed. 如果尝试连接尝试失败,则为false。 If a connection was successfully made, $connection will be a 'MySQL link identifier' which will evaluate to true. 如果连接成功,$ connection将是一个'MySQL链接标识符',它将评估为true。 If you try to echo or print a 'MySQL link identifier', it will print something like "Resource ID #6". 如果您尝试回显或打印“MySQL链接标识符”,它将打印类似“资源ID#6”的内容。 That's what it's supposed to do. 这就是应该做的。

我找到了解决方案,请参阅上面的更新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM