PHP测试远程MySQL服务器是否可用
发布时间:2020-12-13 22:45:24 所属栏目:PHP教程 来源:网络整理
导读:我有一些使用 MySQL的网站.我不是SQL的专家,所以我使用简单的连接,查询等.有时(很少见但发生)只有数据库服务器挂起,或者我忘了打开我的家庭测试mysql.直到它再次运行,服务器挂起尝试连接,最后发生超时错误. 我正在尝试添加一些以前的数据库服务器测试,如“pi
我有一些使用
MySQL的网站.我不是SQL的专家,所以我使用简单的连接,查询等.有时(很少见但发生)只有数据库服务器挂起,或者我忘了打开我的家庭测试mysql.直到它再次运行,服务器挂起尝试连接,最后发生超时错误.
我正在尝试添加一些以前的数据库服务器测试,如“ping”功能: function pingDomain($domain){ $starttime = microtime(true); $file = fsockopen ($domain,80,$errno,$errstr,10); $stoptime = microtime(true); $status = 0; if (!$file) { $status = -1; // Site is down } else { fclose($file); $status = ($stoptime - $starttime) * 1000; $status = floor($status); } return $status; } 但是也没用,php无论如何都挂了.有任何想法吗? 解决方法
简单使用mysql_ping:
<?php set_time_limit(0); $conn = mysql_connect('localhost','mysqluser','mypass'); $db = mysql_select_db('mydb'); /* Assuming this query will take a long time */ $result = mysql_query($sql); if (!$result) { echo 'Query #1 failed,exiting.'; exit; } /* Make sure the connection is still alive,if not,try to reconnect */ if (!mysql_ping($conn)) { echo 'Lost connection,exiting after query #1'; exit; } mysql_free_result($result); /* So the connection is still alive,let's run another query */ $result2 = mysql_query($sql2); ?> 你可能想看看这个: mysql_ping (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |