Mysqli connection trying with different users

Posted by gerardo on Stack Overflow See other posts from Stack Overflow or by gerardo
Published on 2010-05-07T19:35:57Z Indexed on 2010/05/07 19:38 UTC
Read the original article Hit count: 300

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about php5

Related posts about mysqli