如何检查PHP是否已连接到数据库?

时间:2022-12-01 23:07:06

Basically in pseudo code I'm looking for something like

基本上在伪代码中我正在寻找类似的东西

if (connected_to_any_database()) {
    // do nothing
}
else {
    mysql_connect(...)
}

How do I implement

我该如何实施

connected_to_any_database()

6 个解决方案

#1


43  

Have you tried mysql_ping()?

你试过mysql_ping()吗?

Checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted.

检查与服务器的连接是否正常。如果它已经下降,则尝试自动重新连接。

Alternatively, a second (less reliable) approach would be:

或者,第二种(不太可靠)的方法是:

$link = mysql_connect('localhost','username','password');
//(...)
if($link == false){
    //try to reconnect
}

Update: From PHP 5.5 onwards, use mysqli_ping() instead.

更新:从PHP 5.5开始,使用mysqli_ping()代替。

#2


17  

Try using PHP's mysql_ping function:

尝试使用PHP的mysql_ping函数:

echo @mysql_ping() ? 'true' : 'false';

You will need to prepend the "@" to suppose the MySQL Warnings you'll get for running this function without being connected to a database.

你需要在前面加上“@”来假设运行这个函数时你得到的MySQL警告没有连接到数据库。

There are other ways as well, but it depends on the code that you're using.

还有其他方法,但这取决于您正在使用的代码。

#3


3  

mysql_ping

mysql_ping

#4


2  

before... (I mean somewhere in some other file you're not sure you've included)

之前......(我的意思是在某些其他文件中的某个地方,你不确定你是否包括在内)

$db = mysql_connect()

later...

后来...

if (is_resource($db)) {
// connected
} else {
$db = mysql_connect();
}

#5


0  

Baron Schwartz blogs that due to race conditions, this 'check before write' is a bad practice. He advocates a try/catch pattern with a reconnect in the catch. Here is the pseudo code he recommends:

Baron Schwartz博客指出,由于竞争条件,这种“先写后检查”是一种不好的做法。他主张在catch中重新连接一个try / catch模式。这是他推荐的伪代码:

function query_database(connection, sql, retries=1)
   while true
      try
         result=connection.execute(sql)
         return result
      catch InactiveConnectionException e
         if retries > 0 then
            retries = retries - 1
            connection.reconnect()
         else
            throw e
         end
      end
   end
end

Here is his full blog: https://www.percona.com/blog/2010/05/05/checking-for-a-live-database-connection-considered-harmful/

这是他的完整博客:https://www.percona.com/blog/2010/05/05/checking-for-a-live-database-connection-considered-harmful/

#6


-2  

// Earlier in your code
mysql_connect();
set_a_flag_that_db_is_connected();

// Later....
if (flag_is_set())
 mysql_connect(....);

#1


43  

Have you tried mysql_ping()?

你试过mysql_ping()吗?

Checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted.

检查与服务器的连接是否正常。如果它已经下降,则尝试自动重新连接。

Alternatively, a second (less reliable) approach would be:

或者,第二种(不太可靠)的方法是:

$link = mysql_connect('localhost','username','password');
//(...)
if($link == false){
    //try to reconnect
}

Update: From PHP 5.5 onwards, use mysqli_ping() instead.

更新:从PHP 5.5开始,使用mysqli_ping()代替。

#2


17  

Try using PHP's mysql_ping function:

尝试使用PHP的mysql_ping函数:

echo @mysql_ping() ? 'true' : 'false';

You will need to prepend the "@" to suppose the MySQL Warnings you'll get for running this function without being connected to a database.

你需要在前面加上“@”来假设运行这个函数时你得到的MySQL警告没有连接到数据库。

There are other ways as well, but it depends on the code that you're using.

还有其他方法,但这取决于您正在使用的代码。

#3


3  

mysql_ping

mysql_ping

#4


2  

before... (I mean somewhere in some other file you're not sure you've included)

之前......(我的意思是在某些其他文件中的某个地方,你不确定你是否包括在内)

$db = mysql_connect()

later...

后来...

if (is_resource($db)) {
// connected
} else {
$db = mysql_connect();
}

#5


0  

Baron Schwartz blogs that due to race conditions, this 'check before write' is a bad practice. He advocates a try/catch pattern with a reconnect in the catch. Here is the pseudo code he recommends:

Baron Schwartz博客指出,由于竞争条件,这种“先写后检查”是一种不好的做法。他主张在catch中重新连接一个try / catch模式。这是他推荐的伪代码:

function query_database(connection, sql, retries=1)
   while true
      try
         result=connection.execute(sql)
         return result
      catch InactiveConnectionException e
         if retries > 0 then
            retries = retries - 1
            connection.reconnect()
         else
            throw e
         end
      end
   end
end

Here is his full blog: https://www.percona.com/blog/2010/05/05/checking-for-a-live-database-connection-considered-harmful/

这是他的完整博客:https://www.percona.com/blog/2010/05/05/checking-for-a-live-database-connection-considered-harmful/

#6


-2  

// Earlier in your code
mysql_connect();
set_a_flag_that_db_is_connected();

// Later....
if (flag_is_set())
 mysql_connect(....);