gpt4 book ai didi

php - 如何使 php 代码连接到具有两个不同主机的两个不同数据库?

转载 作者:搜寻专家 更新时间:2023-10-30 23:08:21 24 4
gpt4 key购买 nike

详细信息:

我正在为一个游戏创建一个注册页面,该游戏有两个具有不同 ip/host 和不同数据库的服务器,另一个在欧洲托管,另一个在美国托管,我正在计划我的注册页面是当用户成功填写表格并单击“注册”按钮时,用户输入的信息将发送到两个不同的数据库,我搜索与我的问题相关的主题,并按照说明进行修复,但是它没有做任何好事。因此,如果您在这里询问我的代码,那就是。

数据库.php

        <?php

class Database{
private static $instance = null;
private $stmt = null;
private $host = '';
private $dbname = '';
private $username = '';
private $password = '';

public function __construct(){
$dns = "mysql:dbname=".$this->dbname.";host:".$this->host;
$username= $this->username;
$password= $this->password;
$db = null;

$this->db = new PDO($dns,$username,$password);
}

public static function getInstance(){
if(self::$instance == null){
try{
self::$instance = new Database();
}catch(PDOException $e){
echo $e->getCode();
}
}

return self::$instance;
}

public function query($stmt){
$this->stmt = $this->db->prepare($stmt);
}

//get sql statement
public function getStmt(){
return $this->stmt;
}

public function bind($index, $value, $type = null){

if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}

$this->stmt->bindValue($index, $value, $type);
}

public function execute(){
return $this->stmt->execute();
}

public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

public function single()
{
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

public function __destruct(){
$this->db = null;
}
}

如果我没有添加数据库信息,只是为了安全起见,我很抱歉:),所以我想我需要做的就是在 Database.php 上添加一些东西,但我真的不知道是什么.

下面是我注册页面的验证过程代码

RegistrationProcess.php

<?php
include_once 'Input.php';
include_once 'Database.php';
include_once 'Helper.php';

//variables
$db = Database::getInstance();
$username = '';
$password = '';
$email = '';
$country = '';
$city = '';
$question = '';
$answer = '';
$ip = Helper::get_ip_address();
$valid = true;
$msg = '';

if (Input::hasPost()) {
if (Input::post('username') === null) {
$msg = 'Please put your username.';
$valid = false;
$username = Input::post('username');
} else if (Input::post('password') === null || Input::post('con_password') === null ) {
$msg = 'Input password properly!';
$valid = false;
} else if (Input::post('password') !== Input::post('con_password')) {
$msg = 'Password and confirm password must be the same.';
$valid = false;
} else if (Input::post('country') === 'select') {
$msg = 'Select a country.';
$valid = false;
} else if (Input::post('email') === null) {
$msg = 'Put your email.';
$valid = false;
} else if (!filter_var(Input::post('email'), FILTER_VALIDATE_EMAIL)) {
$msg = 'Invalid email.';
$valid = false;
} else if (Input::post('question') === 'select') {
$msg = 'Select a question.';
$valid = false;
} else if (Input::post('answer') === null) {
$msg = 'Put your answer.';
$valid = false;
}

if ($valid) {
$db = Database::getInstance();
$username = Input::post('username');
$password = Input::post('con_password');
$email = Input::post('email');
$country = Input::post('country');
$question = Input::post('question');
$answer = Input::post('answer');

//banned account
$sql = "SELECT * FROM accounts WHERE IP = :ip AND State = 1";
$db->query($sql);
$db->bind(':ip', $ip);
$banned = $db->single();

// > 1 account
$sql = "SELECT * FROM accounts WHERE IP = :ip";
$db->query($sql);
$db->bind(':ip', $ip);
$anotherAccount = $db->resultset();

//already exist name
$sql = "SELECT * FROM accounts WHERE Username = :username";
$db->query($sql);
$db->bind(':username', $username);
$existAccount = $db->single();

if ($existAccount && (count($existAccount) > 0)) {
$msg = 'The Username is already exist.';
$valid = false;
} else if ($banned && (count($banned) > 0)) {
$msg = 'Sorry you cannot register because your banned!';
$valid = false;
} else if ($anotherAccount && (count($anotherAccount) > 5)) {
$msg= 'Sorry you cannot register because you made to many accounts.';
$valid = false;
} else {
$sql = "INSERT INTO accounts (`Username`, `Password`, `IP`, `Email`, `Question`, `answer`, `Country`)
VALUES (:username, :password, :ip, :email, :question, :answer, :country)";

$db->query($sql);

$db->bind(':username', $username);
$db->bind(':password', $password);
$db->bind(':ip', $ip);
$db->bind(':email', $email);
$db->bind(':question', $question);
$db->bind(':answer', $answer);
$db->bind(':country', $country);

if ($db->execute()) {
$msg = "The user has been succesfully added into the database! You can now log into the community and in-game!";
$msg .= " Thank you for registering for West Gaming!<br>";
$msg .= "<span class='management'>- West Gaming Staff</span>";
$valid = true;
unset($_POST);
} else {
$msg = 'There is a problem in our system <br>';
$msg .= 'Please register again with the same data.';
$valid = false;
}
}
}
}

那就是..希望你能帮助我,如果你能帮助我,在此先感谢你。

最佳答案

只需创建 2 个 Database 类实例。

摆脱,公共(public)静态函数 getInstance(){}不要这样做:

$db = Database::getInstance();

相反:

$db1 = new Database();
$db2 = new Database();

Database 构造器编辑成类似的东西

public function __construct($dns, $username, $password){
$this->db = new PDO($dns,$username,$password);
}

关于php - 如何使 php 代码连接到具有两个不同主机的两个不同数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23380477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com