php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43866 Can't define the connect timeout on mysqli extension class
Submitted: 2008-01-16 14:23 UTC Modified: 2009-03-13 22:54 UTC
From: basilio dot vera at softonic dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.2.5 OS: Linux RHEL4
Private report: No CVE-ID: None
 [2008-01-16 14:23 UTC] basilio dot vera at softonic dot com
Description:
------------
I can't change the "MYSQLI_OPT_CONNECT_TIMEOUT" flag inside an extended class of mysqli.

This is not really a bug, but a missed feature!


Reproduce code:
---------------
// I want some similar to this:
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
$mysqli->real_connect('localhost', 'my_user', 'my_password', 'world');

// My class that does many magic things.
class mysqli2 extends mysqli
{
    public function __construct( $host, $user, $pass, $db )
    {
        parent::__construct( $host, $user, $pass, $db );
        // Too late, the connection has been done before!
        $this->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
    }
}
// If I put the options call before the parent::__construct() call I get 'Couldn't fetch DbLink' error.

// And, obviously, this does not work.
class mysqli3 extends mysqli_init {}

Expected result:
----------------
The connect timeout has been changed to 5 seconds.

Actual result:
--------------
The connect timeout has not been changed.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-16 14:34 UTC] basilio dot vera at softonic dot com
Sorry, the error message was:

'Couldn't fetch mysqli2'
 [2008-01-17 00:19 UTC] basilio dot vera at softonic dot com
Hi, I've found a solution for this problem. It can be solved with:

class mysqli2 extends mysqli
{
    public function __construct( $host, $user, $pass, $db )
    {
        $this->init();
        $this->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
        $this->real_connect( $host, $user, $pass, $db );
    }
}

But you may consider review your docs, because it's and undocumented feature!
 [2008-01-17 17:29 UTC] uw@php.net
Give me a hint, what's up with the documentation?

http://us2.php.net/mysqli_connect shows that calling constructor of the mysqli class is equal to calling mysqli_connect(). As you know, you need to call mysqli_options() before a connection has been established. In your reproduceable code you suggest a derived class which explicitly calls the parent constructor to create a connection. Once the parent constructor has been called you try to call mysqli_options() (mysqli::options()). That's too late.

Your second example gets it right. How could we improve the documentation? Thanks!

Ulf
 [2008-01-17 17:54 UTC] basilio dot vera at softonic dot com
For example with an example inside the mysqli_init() or mysqli_options() function docs:

class mysqli2 extends mysqli
{
    public function __construct( $host, $user, $pass, $db )
    {
        parent::init();
        parent::options( MYSQLI_OPT_CONNECT_TIMEOUT, 5 );
        parent::real_connect( $host, $user, $pass, $db );
    }
}

Regards.
 [2008-01-17 18:35 UTC] uw@php.net
Not a bug, solution is given, documentation "flaw" discussed.
 [2009-03-13 22:54 UTC] danielc@php.net
Examples added to manual.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 04:01:29 2024 UTC