我知道如何将2台服务器连接到数据库有答案,但我的情况有点不同,所以不要只是将我重定向到另一个答案。我的联系不是简单的单行,我正在寻找最好的方法(放弃课程,做一些完全不同的事情,一种通过该课程连接2的方法等)。
所以这就是发生的事情。我目前在国外实习,我被分配了一个简单的任务,我必须使用1台服务器,完成所有工作,这似乎是故事的结尾。然而,一旦我们将所有内容都移动到真正的主机上,我发现我有2台服务器来自而不是1台。
这就是我的1服务器连接的样子:
class.database.inc
<?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;
在config.php文件中我只有这个(删除了值,它现在仍然有效):
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;
也许一些额外的信息会有所帮助所以这就是我使用1函数从SQL获取数据的地方(它们现在都失败了所以我猜1个例子就足够了)。
users.php:
//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时得到的结果:
“数据库查询失败:
上次SQL查询:SELECT COUNT(*)FROM table_name“ (当我直接在phpmyadmin中输入时,同样的SQL查询有效。)
是的..只要我只使用1台服务器(页面顶部的第一个块),一切正常。
答案 0 :(得分:1)
为什么依靠define
常量语句?您可以编写.ini文件并使用$array = parse_ini_file(filename)读取配置。这样做,您可以使用特定配置的文件名(或更高级的部分名称)来调用构造函数。
因为你在谈论“最好的方法” - 如果你真的需要mysql_real_escape_string
(动态$table_name
一个原因,那就没有别的办法了)处理它可能不存在的情况你应该(如果没有)拒绝任何访问或至少提供带有正则表达式的polyfill。 mysqli
的{{1}}效果更好,请考虑使用mysql
。尽可能使用参数化预准备语句来提高安全性。
用于提供静态方法的类的误用是一个过时的概念,因为在较新的PHP版本中,上下文分离可以通过命名空间很好地处理。