php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50772 mysqli constructor without parameters does not return a working mysqli object
Submitted: 2010-01-15 19:34 UTC Modified: 2010-01-25 13:24 UTC
From: elmo dot allen at iki dot fi Assigned: mysql (profile)
Status: Closed Package: MySQLi related
PHP Version: 5.*, 6 OS: *
Private report: No CVE-ID: None
 [2010-01-15 19:34 UTC] elmo dot allen at iki dot fi
Description:
------------
Documentation states that calling mysqli::__construct() without parameters (i.e. new mysqli()) should do the same as calling mysqli_init(). It does not, however, return a working mysqli object, but all the following calls to its methods only return Warning "Couldn't fetch mysqli".

Using mysqli_init() directly works as intended, but using it makes making derived mysqli classes much harder, if trying to use real_connect(), because derived classes can only call their parent's constructor, not mysqli_init().

The following code is by design trying a connection and failing (with MySQL server not running) and gracefully admitting it with an error message (warnings not tried to be silenced here).

Not working in PHP 5.2.8 either. No newer snapshots for Windows seem to be available.

Reproduce code:
---------------
<?php

// Does not return a working object
$db1 = new mysqli();

// These calls fail
$db1->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3);
$db1->real_connect("localhost");
if(mysqli_connect_error()) echo "error 1";
else echo "ok 1";

// This works
$db2 = mysqli_init();

// And these calls work as intended (give correct error msgs when connection fails)
$db2->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3);
$db2->real_connect("localhost");
if(mysqli_connect_error()) echo "error 2";
else echo "ok 2";

?>

Expected result:
----------------
Warning:  mysqli::real_connect() [mysqli.real-connect]: [2002] A connection attempt failed because the connected party did not  (trying to connect via tcp://localhost:3306) in C:\Users\Elmo\Documents\Web server\test.php on line 7



Warning:  mysqli::real_connect() [mysqli.real-connect]: (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
 in C:\Users\Elmo\Documents\Web server\test.php on line 7

error 1

Warning:  mysqli::real_connect() [mysqli.real-connect]: [2002] A connection attempt failed because the connected party did not  (trying to connect via tcp://localhost:3306) in C:\Users\Elmo\Documents\Web server\test.php on line 13



Warning:  mysqli::real_connect() [mysqli.real-connect]: (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
 in C:\Users\Elmo\Documents\Web server\test.php on line 13

error 2


Actual result:
--------------
Warning:  mysqli::options() [mysqli.options]: Couldn't fetch mysqli in C:\Users\Elmo\Documents\Web server\test.php on line 6



Warning:  mysqli::real_connect() [mysqli.real-connect]: Couldn't fetch mysqli in C:\Users\Elmo\Documents\Web server\test.php on line 7

ok 1

Warning:  mysqli::real_connect() [mysqli.real-connect]: [2002] A connection attempt failed because the connected party did not  (trying to connect via tcp://localhost:3306) in C:\Users\Elmo\Documents\Web server\test.php on line 13



Warning:  mysqli::real_connect() [mysqli.real-connect]: (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
 in C:\Users\Elmo\Documents\Web server\test.php on line 13

error 2


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-25 13:23 UTC] svn@php.net
Automatic comment from SVN on behalf of andrey
Revision: http://svn.php.net/viewvc/?view=revision&revision=293976
Log: Fix for bug#50772
mysqli constructor without parameters does not return a working mysqli object
 [2010-01-25 13:24 UTC] andrey@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Thank you!
Fixed in 5.2, 5.3 and HEAD
 [2010-02-04 20:28 UTC] svn@php.net
Automatic comment from SVN on behalf of johannes
Revision: http://svn.php.net/viewvc/?view=revision&revision=294543
Log: Merge mysql changes

293815 Add possibility to restrain the number of levels (andrey)
293816 decrement correctly! (andrey)
293976 Fix for bug#50772  (andrey)
294302 Tests related to #50727 (uw)
294313 Fix a bug in mysqlnd where a pointer was not incremented (andrey)
294317 Fix second bug in that code, + instead of += thus not increasing (andrey)
294337 No idea which recent change has cause this little API change, however it is not (uw)
294421 Pass tsrmls to all functions, which might need it in the future. (andrey)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 07 14:01:27 2024 UTC