PHP 如果断开连接则重新连接的mysql连接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 如果断开连接则重新连接的mysql连接相关的知识,希望对你有一定的参考价值。

$local = false;
$master = false;
$host = array('local'=>'localhost','master'=>'192.168.1.1');
function db_conn($type)
{
        global $local,$master,$host;
//      echo 'in db_conn';
        if(${$type} && ${$type}->ping())
        {
//              echo 'connection already exists and is alive';
                return ${$type};
        }
        else
        {
//              echo 'connection does not exist, attempting to connect';
                ${$type} = new mysqli($host[$type],'user','password','database');
                if(${$type})
                {
                        return ${$type};
                }
                else
                {
                        err('Could not connect to '.${$type}.' database.'.mysqli_connect_error());
                        return false;
                }
        }
}

以上是关于PHP 如果断开连接则重新连接的mysql连接的主要内容,如果未能解决你的问题,请参考以下文章