/ Published in: PHP
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!
$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!
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$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; } } }