ZF-5342: Mysqli::__connect() forget to reset internal mysqli _connection object when connection failed

Description

Just found a minor issue with MySQLi::connect().

The test made to see if the mysqli connection succeed is based on 2 conditions : $this->_connection and mysqli_connect_errno(). In case of failure with mysql_connect_errno(), we need to clear/unset the value of $this->_connection (which is a "false" mysqli object = an empty mysqli object with no real link to the database - sorry for approximative english :).

The problem is that the entire class relies on existence of $this->_connection to see if the connection is active. In some circumstances, (i.e. ZF-3984) the connection exception can be catched by a calling class and ignored, all subsequent calls will think that the connection is effective (because Mysqli::_connection is an object) and write warnings which are undesirable and hard to debug.

I think fixing the bug is just one line... read the proposed solution below and hope that it will work

Thanks to Zend guys, it's an amazing job you do...

Seb

Solution attempt :

[original]


if ($this->_connection === false || mysqli_connect_errno()) {
         require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
         throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
}

[patched]


if ($this->_connection === false || mysqli_connect_errno()) {
       // THE connection error was given with mysqli_connect_errno, the value of $this->_connection is a "invalid" mysqli object. So I reset it
        $this->_connection = false; 
        require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
        throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
}

Comments

An example of the same issue when database connection fails because of "Too many open links".

Solution with formatted code, for ZF 1.7.1 Zend_Db_Adapter_Mysqli::_connect() method



        if ($this->_connection === false || mysqli_connect_errno()) {
            /**
             * @see Zend_Db_Adapter_Mysqli_Exception
             */
            {color:red}
           // unsetting invalid mysql object
            $this->_connection = false;;
           {color}
            require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
            throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
        }

PS: take a look at ZF-3984, this attempt brings a solution for that (mysqli_real_escape wont't be called, the exception will be thrown as expected), but there's maybe some reasons to look deeper, to see why the quote method was called even if the connection failed (lazy connect mode ?).

Is this still active? The description seems to be outdated.

Solved by ZF-3984