2つのサーバをデータベースに接続する方法はありますが、私の状況は少し異なりますので、別の答えにリダイレクトしないでください。私の接続は単純な1ライナーではなく、進歩するための最善の方法を探しています(クラスをあきらめて全く別のことをする、クラスを2つ接続する方法など)。PHPで2つのサーバをデータベースクラスで接続する
これは何が起こったかです。私は現在、海外のインターンであり、私は1台のサーバーで作業しなければならなかったシンプルな作業を割り当てられました。すべてが完了していて、それはストーリーの終わりと思われました。
class.database:私たちは実際のホストにすべてを移動したら、しかし、それはテーブルだけではなく、1
のどこから来た私は2つのサーバーを持っていることが判明したので、これは私の1つのサーバー接続がどのように見えるかです。株式会社
私はこれ(それはとにかく、今のように動作し、値を削除)を持つのconfig.phpファイルで<?php
require_once(LIB_PATH . "config.php");
class MySQLDatabase {
private $connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exists;
function __construct() {
$this->open_connection();
$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exists = function_exists("mysql_real_escape_string");
}
public function open_connection() {
$this->connection = mysqli_connect(SERVER_ADDR, SERVER_USER, SERVER_PWD, SERVER_DB);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$db_select = $this->connection->query("SELECT DATABASE()");
if (!$db_select) {
printf("Database selection failed: %s\n", mysqli_connect_error());
exit();
}
}
}
public function close_connection() {
if (isset($this->connection)) {
$this->connection->close();
unset($this->connection);
}
}
public function query($sql) {
$this->last_query = $sql;
$result = $this->connection->query($sql);
$this->confirm_query($result);
return $result;
}
public function escape_value($value) {
if ($this->real_escape_string_exists) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if ($this->magic_quotes_active) {
$value = stripslashes($value);
}
$value = $this->connection->real_escape_string($value);
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if (!$this->magic_quotes_active) {
$value = addslashes($value);
}
// if magic quotes are active, then the slashes already exist
}
return $value;
}
// "database-neutral" methods
public function fetch_array($result_set) {
return $result_set->fetch_array(MYSQLI_ASSOC);
}
public function num_rows($result_set) {
return $result_set->num_rows;
}
public function insert_id() {
// get the last id inserted over the current db connection
return $this->connection->insert_id;
}
public function affected_rows() {
return $this->connection->affected_rows;
}
private function confirm_query($result) {
if (!$result) {
$output = "Database query failed: " . mysqli_connect_error() . "<br /><br />";
$output .= "Last SQL query: " . $this->last_query;
die($output);
}
}
}
$database = new MySQLDatabase();
$db = & $database;
:
defined("SERVER_ADDR") ? null : define("SERVER_ADDR",);
//MYSQL username
defined("SERVER_USER") ? null : define("SERVER_USER",);
//MYSQL password
defined("SERVER_PWD") ? null : define("SERVER_PWD",);
//MYSQL database name
defined("SERVER_DB") ? null : define("SERVER_DB",);
そこで質問がための最良の方法は何か、であるが進む。別の "config2.php"タイプのファイルを追加してクラス内のビットを変更して、これらの関数が両方のクラスで動作するようにする方法はありますか?
いつものように、本当に感謝している回答に感謝します。
更新:
これは私が試したもので、動作しませんでした。必要なSQL関数を実行すると、正しい表が表示されますが、関数確認クエリは毎回失敗します。
<?php
require_once(LIB_PATH . "config.php");
class MySQLDatabase {
private $connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exists;
public $server;
public $user;
public $pw;
public $database;
function __construct() {
$this->open_connection();
$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exists = function_exists("mysql_real_escape_string");
}
public function open_connection() {
$this->connection = mysqli_connect($this->server, $this->user, $this->pw, $this->database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$db_select = $this->connection->query("SELECT DATABASE()");
if (!$db_select) {
printf("Database selection failed: %s\n", mysqli_connect_error());
exit();
}
}
}
public function close_connection() {
if (isset($this->connection)) {
$this->connection->close();
unset($this->connection);
}
}
public function query($sql) {
$this->last_query = $sql;
$result = $this->connection->query($sql);
$this->confirm_query($result);
return $result;
}
public function escape_value($value) {
if ($this->real_escape_string_exists) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if ($this->magic_quotes_active) {
$value = stripslashes($value);
}
$value = $this->connection->real_escape_string($value);
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if (!$this->magic_quotes_active) {
$value = addslashes($value);
}
// if magic quotes are active, then the slashes already exist
}
return $value;
}
// "database-neutral" methods
public function fetch_array($result_set) {
return $result_set->fetch_array(MYSQLI_ASSOC);
}
public function num_rows($result_set) {
return $result_set->num_rows;
}
public function insert_id() {
// get the last id inserted over the current db connection
return $this->connection->insert_id;
}
public function affected_rows() {
return $this->connection->affected_rows;
}
private function confirm_query($result) {
if (!$result) {
$output = "Database query failed: " . mysqli_connect_error() . "<br /><br />";
$output .= "Last SQL query: " . $this->last_query;
die($output);
}
}
}
$database = new MySQLDatabase();
$database->server = SERVER_ADDR;
$database->user = SERVER_USER;
$database->pw = SERVER_PWD;
$database->database = SERVER_DB;
$db = & $database;
おそらく、いくつかの追加の情報は、私はSQL(それらはすべてが今では、私は1例では、スタートのために十分であると思います失敗)からのデータを取得するために、1つの機能を使用しているので、これは役立つだろう。
//more code
$max_users = User::count_all(); //max users
//more code
class.database_query.inc:
require_once(LIB_PATH . "class.database.inc");
class DatabaseObject {
protected static $table_name;
//more functions
//the one in use:
public static function count_all() {
global $database;
$sql = "SELECT COUNT(*) FROM " . static::$table_name;
$result_set = $database->query($sql);
$row = $database->fetch_array($result_set);
return array_shift($row);
}
//more functions
}
そして、これはusers.phpをロードするときに、私が得る結果である:
users.php「データベースのクエリが失敗しました:
最後のSQLクエリ:SELECT COUNT(*)FROM table_name " (That phpmyadminに直接入力すると同じSQLクエリが動作します)。
そして、私が1つのサーバー(ページの最初のブロック)を使用している限り、それはすべて機能します。
クラスのコンストラクタで設定を渡します。複数のインスタンスを持つことができます。 –
はい、私はそれをやろうとしましたが、失敗しました。私がしたことを示すために投稿を更新しました – Rauno