简体   繁体   English

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

[英]How do I check if PHP is connected to a database already?

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()

Have you tried mysql_ping() ?你试过mysql_ping()吗?

Update: From PHP 5.5 onwards, use mysqli_ping() instead.更新:从 PHP 5.5 开始,使用mysqli_ping()代替。

Pings a server connection, or tries to reconnect if the connection has gone down. Ping 一个服务器连接,或者在连接断开时尝试重新连接。

 if ($mysqli->ping()) { printf ("Our connection is ok!\\n"); } else { printf ("Error: %s\\n", $mysqli->error); }

Alternatively, a second (less reliable) approach would be:或者,第二种(不太可靠的)方法是:

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

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.还有其他方法,但这取决于您使用的代码。

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();
}

Baron Schwartz blogs that due to race conditions, this 'check before write' is a bad practice. Baron Schwartz 在博客中表示,由于竞争条件,这种“写前检查”是一种不好的做法。 He advocates a try/catch pattern with a reconnect in the catch.他提倡在捕获中reconnect的 try/catch 模式。 Here is the pseudo code he recommends:这是他推荐的伪代码:

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-thinked-harmful/

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

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

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

相关问题 如何使用PHP和Mysql检查数据库中是否已存在相同的标签? - How do I check if the same tag was already in database with PHP and Mysql? 如何检查我是否连接到数据库? - How do I check if I'm connected to database? 如果数据库中已存在URL,如何使用PHP检查? - How can I check using PHP if URL already exists in database? 如何检查数据库中已经存在的值? - How do I check database for a value that already exists? 当数据库连接/未连接时,如何使用MySQL / PHP显示不同的图像? - How do I display different images using MySQL/PHP when database connected/not connected? 如何在Codeigniter中使用已连接数据库的数据库连接? - how can i use database connection of the already connected database in codeigniter? 如何使用 PHP 正确检查 MongoDB Atlas 数据库中的现有数据。 我总是知道名字已经存在 - How do you properly check for an existing data in MongoDB Atlas database using PHP. I always get thet the name already exists PHP如何在MySQL数据库中检查电子邮件? - PHP how to check for email already in MySQL database? 如何使用PHP检查数据库中是否已存在数据? - How to check if data is already in database using PHP? 如何检查PHP中的数据库中是否已存在url? - how to check if url already exists in database in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM