php db class with mysqli, which support multiple connections

99封情书 提交于 2019-12-06 05:47:12

First thing that comes to mind, is a container class, that stores MySQLi object in it. Something like this:

class MySQLiContainer extends SplObjectStorage{
  public function newConnection($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) {
    $mysqli = new mysqli($host, $username, $passwd, $dbname, $port, $socket);
    $this->attach($mysqli);
    return $mysqli;
  }
}

//usage

$mysqliContainer = new MySQLiContainer();

$c1 = $mysqliContainer->newConnection('localhost','root','root','localDatabase');
$c1->query('SELECT ....');

$c2 = $mysqliContainer->newConnection('mysql.remotehost.net','hackermom','bobbytables','schoolDatabase');

$name = 'Robert\'); DROP TABLE students;--';

$c2->multi_query("SELECT * FROM students WHERE name = '$name'");

Without knowing more about functionality required, it's hard to say if this is a good idea though ;)

More info about SplObjectStorage class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!