PHP测试远程MySQL服务器是否可用

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP测试远程MySQL服务器是否可用脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些使用 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

脚本宝典总结

以上是脚本宝典为你收集整理的PHP测试远程MySQL服务器是否可用全部内容,希望文章能够帮你解决PHP测试远程MySQL服务器是否可用所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。