我的代码中有些部分需要检查是否已经建立了mysql连接。下面的代码使用了if(self::$connection)
,但是self::connection
似乎总是返回"Resource id #6"而不是布尔值——我做错了什么?
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重新设置为false...
static function disconnect(){
mysql_close(self::$connection);
self::$connection = FALSE;
}