如果已断开连接,则重新连接的mysql连接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果已断开连接,则重新连接的mysql连接相关的知识,希望对你有一定的参考价值。
usage is simple:$result = mysqli_query($query,db_conn('local')); for instance.
if written in patterns, this should probably be a singleton, but I haven't bothered converting it - feel free to comment with your modification though!
$local = false; $master = false; 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 { return false; } } }
以上是关于如果已断开连接,则重新连接的mysql连接的主要内容,如果未能解决你的问题,请参考以下文章