PHP OOP - 连接到不同的主机

时间:2017-04-26 03:28:20

标签: php oop

我遇到连接到不同主机的问题。

请参阅下面的连接类。

class Connection
{
    protected static $connectDb;
    protected $dbHost;
    protected $dbUser;
    protected $dbPass;
    protected $dbName;
    protected $charSet;

    public function __construct ($dbHost, $dbUser, $dbPass, $dbName, $charSet){
        $this->dbHost = $dbHost;
        $this->dbUser = $dbUser;
        $this->dbPass = $dbPass;
        $this->dbName = $dbName;
        $this->charSet = $charSet;
    }

    /**
    * Connect to the database
    *
    * @return bool false on failure / mysqli MySQLi object instance on success
    */
    public function connect()
    {

        if (!isset(self::$connectDb)) {
                self::$connectDb = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
        }

        if (self::$connectDb->connect_errno) {
          printf("Connection failed: %s\n", self::$connectDb->connect_error);
          exit();
        }

        if ($this->charSet) {
            if (!self::$connectDb->set_charset($this->charSet)) {
                printf("Error loading character set ".$this->charSet.": %s\n", self::$connectDb->error);
                exit();
            } else {
                // printf("Current character set: %s\n", self::$connectDb->character_set_name());
            } 
        }

        return self::$connectDb;
    }

    /**
    * Query the database
    *
    * @param $query The query string
    * @return mixed The result of the mysqli::query() function
    */
    public function query($query)
    {
        $conn = $this->connect();

        $result = $conn->query($query);

        return $result;
    }
}

我创建如下连接: -

$conn1 = new Connection('localhost', '[user1]', '[password1]', '[db_name1]', 'utf8');

$conn2 = new Connection('xxx.xxx.xx.xx', '[user2]', '[password2]', '[db_name2]', 'utf8');

但是当我在$ conn2上运行查询时,它仍然连接并运行$ conn1。

示例查询: -

$result = $conn1->query([select_query_from_db_name1]);

foreach($result as $value) {
    $conn2->query([insert_into_db_name2]);
}

希望任何人都可以指出我的问题所在,因为我还在学习oop。

提前致谢。

2 个答案:

答案 0 :(得分:0)

正如评论中所提到的,static关键字意味着在这种情况下,对于所有类的实例,变量将保持不变,因此删除它将允许两个不同的Connection个实例有两种不同的联系。

但是,对于静态来说,这是一个非常不直观的用例,因为静态属性更像是类常量。例如,您可以将计算器类的税率定义为静态私有属性,并在任何地方使用self::TAX_RATE,而不使用属性的“成本”。

但是,如果我可以,我相信您可以通过删除此连接类来进一步改进您的代码。它实际上没有什么比基本的\mysqli对象更好,实际上维护起来会很麻烦。

这里实现的query()功能非常糟糕:它阻止了prepared statements的使用,让你对sql注入完全开放。

实施的手动错误检查也没用,在应用程序级别设置错误报告已经允许连接告诉您它是否无法连接,除了重写所有内容之外还有更好的细节。

答案 1 :(得分:0)

你应该直接将它用于构造函数,

self::$connectDb = new mysqli($this->dbHost, $this->dbUser,
 $this->dbPass, $this->dbName);

您也可以将它用于构造函数,

if (!isset(self::$connectDb)) {             
   self::$connectDb = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
        }

但在这种情况下应该在

中使用参数$ newConn = true
$conn1 = new Connection('localhost', '[user1]', '[password1]', '[db_name1]', 'utf8', $newConn);

现在你的构造函数签名应该是

public function __construct ($dbHost, $dbUser, $dbPass, $dbName, $charSet, $newCon = false){
        $this->dbHost = $dbHost;
        $this->dbUser = $dbUser;
        $this->dbPass = $dbPass;
        $this->dbName = $dbName;
        $this->charSet = $charSet;

 if (!isset(self::$connectDb) || $newConn) {
                 self::$connectDb = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
         }


    }