如何使php代码使用两个不同的主机连接到两个不同的数据库? [英] How to make php code connect to two different database with two different host?

查看:61
本文介绍了如何使php代码使用两个不同的主机连接到两个不同的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

详细信息:

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

I am creating a registration page for a game, and the game has two server with different ip's/host and different database, the other one is hosted on Euro and the other is hosted on USA, and what I am planning on my registration page are when the user successfully fill up the form and click "Register" button, the info the user put will be send to the two different database, I search a topic related to my problem, and follow the instructions to fixed it, but it didn't do any good thing. So if you will ask on my code here it is.

Database.php

Database.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上添加一些内容,但是我真的不知道什么。

I'm sorry if i didn't add the database info, just for security sake :), so I think all I need to do is to add something on the Database.php , but I really don't know what.

现在这是我的注册页面的验证过程代码

now here is the validation process code of my registration page

RegistrationProcess.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;
            }
         }
      }
   } 

那就是..希望您能为我提供帮助,如果您愿意,请提前谢谢。

and that is.. hope you can help me and if you do, Thanks in advance.

推荐答案

只需创建2个数据库类实例。

公共静态函数getInstance(){}
不要这样:

Rid of, public static function getInstance(){} Dont do like this:

$db = Database::getInstance();

相反:

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

数据库构造函数编辑为类似

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

这篇关于如何使php代码使用两个不同的主机连接到两个不同的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆