そこで、多数の mysql 接続を管理する簡単な方法を作ろうとしています。
私は 2 つの基本的なクラスを持っています:server
とConnections
.
サーバーは、表示されているデータの行を保持するようにセットアップされています。(これを行うより良い方法はありますか?)
接続では、ID で接続を呼び出して接続を作成し、クエリを実行できるようにしたいと考えています。
これは mysql ライブラリで動作しますが、ライブラリを使用しようとすると、接続ハンドルとして 渡されmysqli
ません。動作しますが、動作しません。 $mysqli
$msi
$mysqli->query()
$msi->query()
PHP Fatal error: Call to undefined method Connections::query()
Connections クラスを mysqli で拡張/実装する$mysqli
か、別の方法で返す必要がありますか?
ありがとうございました!
<?php
class server {
public $id, $type, $subid, $host, $user, $pw, $port, $db;
public function __construct($id,$type,$subid,$host,$user,$pw,$port,$db) {
$this->id = $id;
$this->type = $type;
$this->subid = $subid;
$this->host = $host;
$this->user = $user;
$this->pw = $pw;
$this->port = $port;
$this->db = $db;
}
}
class Connections {
public $servers;
function __construct($id) {
$this->servers[] = new server("mysql","mysql","main","hostname","username","password","3306","dbname");
$this->servers[] = new server("mysql2","mysqli","main","hostname","username","password","3306","dbname");
$rt = null;
foreach($this->servers as $server) {
if ($id == $server->id) {
$rt = $server;
break;
}
}
if($rt->type == "mysql"){
$con = mysql_connect($rt->host,$rt->user,$rt->pw);
mysql_select_db($rt->db, $con);
if($con) { return $con; }
}
elseif($rt->type == "mysqli"){
$mysqli = new mysqli($rt->host, $rt->user, $rt->pw, $rt->db, $rt->port);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
else{
return $mysqli;
}
}
}
}
/*
### this works
$nc = new Connections("mysql");
$q = "select 1+2 as asdf";
$r = mysql_query($q);
while($row = mysql_fetch_array($r)){
echo $row['asdf']."\n";
}
*/
#### this does not work
$msi = new Connections("mysql2");
$res = $msi->query("SELECT 2+2 as asdf");
while($row = $res->fetch_assoc()){
echo $row['asdf']."\n";
}
?>
#
編集 - >ここの別の投稿の助けを借りて、これを別の方法で機能させることができました
ここに私の改訂されたコードがあります:
<?php
class server {
public $id, $type, $subid, $host, $user, $pw, $port, $alt;
public function __construct($id,$type,$subid,$host,$user,$pw,$port,$alt) {
$this->id = $id;
$this->type = $type;
$this->subid = $subid;
$this->host = $host;
$this->user = $user;
$this->pw = $pw;
$this->port = $port;
$this->alt = $alt;
}
}
class MySQLiContainer extends SplObjectStorage{
var $server, $servers;
function __construct($id) {
$this->servers[] = new server("mysql","mysql","main","hostname","username","password","3306","dbname");
$this->servers[] = new server("mysql2","mysqli","main","hostname","username","password","3306","dbname");
foreach($this->servers as $server) {
if ($id == $server->id) {
$this->server = $server;
break;
}
}
}
public function newConnection() {
$mysqli = new mysqli($this->server->host, $this->server->user, $this->server->pw, $this->server->alt, $this->server->port);
$this->attach($mysqli);
return $mysqli;
}
}
//usage
$mysqliContainer = new MySQLiContainer("mysql2");
$c1 = $mysqliContainer->newConnection();
$res = $c1->query('SELECT 2+4 as asdf');
while($row = $res->fetch_assoc()){
echo $row['asdf']."\n";
}
echo $c1->host_info . "\n";
$ms2 = new MySQLiContainer("mysql");
$c2 = $ms2->newConnection();
$r = $c2->query('SELECT 2+4 as dfdf');
while($ra = $r->fetch_assoc()){
echo $ra['dfdf']."\n";
}
echo $c2->host_info . "\n";
?>