|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-01-25 13:23 UTC] svn@php.net
[2010-01-25 13:24 UTC] andrey@php.net
[2010-02-04 20:28 UTC] svn@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 00:00:01 2025 UTC |
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