Farklı kullanıcılar ile çalışıyor Mysqli bağlantısı

1 Cevap php

Ben bağlantı başarısız olursa, başka bir kullanıcı ile bağlantı kapasitesine sahip bir PHP sınıfı uzanan mysqli oluşturmak çalışıyorum. Muhtemelen kodu ile açıklamak daha kolaydır:

public function __construct() {
    $users = new ArrayObject(self::$user);
    $passwords = new ArrayObject(self::$pass);
    $itUser = $users->getIterator();
    $itPass = $passwords->getIterator();

    parent::__construct(self::$host, $itUser->current(), $itPass->current(), self::$prefix.self::$db);
    while($this->connect_errno && $itUser->valid()){
        $itUser->next();
        $itPass->next();
        $this->change_user($itUser->current(), $itPass->current(), self::$prefix.self::$db);
    }

    if($this->connect_errno)
        throw new Exception("Error", $this->connect_errno);
}

$user and $pass are static variables containing arrays of users and passwords. If the first user fails to connect, I try with the next one.

Burada sorun, $this->connect_errno ile. Bu mysqli bulamadığını söylüyor.

Buna bir çözüm var mı ya da ben bir fabrika sınıf oluşturmak gerekir?

1 Cevap

Emin neden gösterildiği gibi, ancak yine de, mysqli_error() hata işleme için kullanabileceğiniz, nesneyi (ipucu, kimseyi?) Bulmak etmez this article.

while(!mysqli_error($this) && $itUser->valid()) {
    // (...)
}

ve

if(mysqli_error($this)) {
    throw new Exception(mysqli_error($this), mysqli_errno($this));
}