开发者

Mysqli connection trying with different users

开发者 https://www.devze.com 2022-12-29 19:22 出处:网络
I\'m trying to create a PHP class extending mysqli that is capable of connecting with another user if the connection fails. It is probably easier to explain with code:

I'm trying to create a PHP class extending mysqli that is capable of connecting with another user if the connection fails. It is probably easier to explain with code:

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.

The problem here is with $this->connect_errno. It says it cannot find Mysqli.

Is there any solution to this or should I create a Factory class?


Not sure why it doesn't find the object (hint, anybody?), however you can still use mysqli_error() for error handling, as shown in this article.

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

and

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

精彩评论

暂无评论...
验证码 换一张
取 消