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

Issue Type: Bug Created: 2008-12-23T06:36:25.000+0000 Last Updated: 2011-08-03T15:33:11.000+0000 Status: Resolved Fix version(s): - 1.8.2 (27/May/09)

Reporter: Sébastien Vanvelthem (belgattitude) Assignee: Satoru Yoshida (satoruyoshida) Tags: - Zend_Db_Adapter_Mysqli

Related issues: - ZF-3984



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...


Solution attempt :


<pre class="highlight">
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());


<pre class="highlight">
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());


Posted by Sébastien Vanvelthem (belgattitude) on 2008-12-23T06:38:31.000+0000

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

Posted by Sébastien Vanvelthem (belgattitude) on 2008-12-23T06:53:59.000+0000

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

<pre class="highlight">

        if ($this->_connection === false || mysqli_connect_errno()) {
             * @see Zend_Db_Adapter_Mysqli_Exception
           // unsetting invalid mysql object
            $this->_connection = false;;
            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 ?).

Posted by old of Satoru Yoshida ( on 2009-05-11T23:07:31.000+0000

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

Posted by old of Satoru Yoshida ( on 2009-05-15T18:32:46.000+0000

Solved by ZF-3984

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.