Doc #80529 [Nab]: $mysqli->connect_errno may not work

From: Date: Fri, 18 Dec 2020 00:32:05 +0000
Subject: Doc #80529 [Nab]: $mysqli->connect_errno may not work
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18284@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80529&edit=1 ID: 80529 Updated by: requinix@php.net Reported by: bugs dot php dot net at latter dot org Summary: $mysqli->connect_errno may not work Status: Not a bug Type: Documentation Problem Package: MySQLi related PHP Version: Irrelevant Assigned To: cmb Block user comment: N Private report: N New Comment: The original code in the bug report is fine, but it's not the code you were using and it won't create an undefined property warning you mentioned. Procedural form is a regular function call: returns an object if successful, false if not. OOP form is a regular class instantiation: guaranteed to return an object... as long as the construction completes and no exception is thrown. In your real code, apparently an exception is being thrown which means *nothing* is returned - the construction didn't complete. And nothing returned means no $mysqli variable. If you want to check for a connection error in a catch{} then, lacking an object, you need to use mysqli_connect_errno(). > are there conditions under which a failed connection returns false and other > conditions under which it returns a failed-connection object? "new mysqli(...)" will never return false. That doesn't mean it will always return, but when it does, it will give you an object. > Should the Note be moved up to the Return value, and the existing content in > the return value be prefixed with "Procedural only:"? You're touching on something that isn't just about this page but about all places where there are OOP and procedural syntaxes. - The possibility of returning false should be covered in the function declaration at the top of the page, in that gives a return type of "<class>|false" - "new <class>(...)" invokes __construct, whose declaration should not show any return type at all (because that's how constructors are), and the reader should already be aware of how object instantiation works Any commentary about return values only ever applies to the procedural form because that's the only place a return value makes sense to talk about. Previous Comments: ------------------------------------------------------------------------ [2020-12-18 00:09:38] a at b dot c dot de Ah, got it; mysqli_connect may return false, "new mysqli" always returns an object even if the connection fails. No, wait: "Returns an object which represents the connection to a MySQL Server, or false on failure." Does this apply to both forms or only to mysqli_connect? Because further down it says "OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples." Or are there conditions under which a failed connection returns false and other conditions under which it returns a failed-connection object? Should the Note be moved up to the Return value, and the existing content in the return value be prefixed with "Procedural only:"? ------------------------------------------------------------------------ [2020-12-17 23:58:06] a at b dot c dot de This should probably be reclassified as a Doc bug, because the original code is lifted verbatim from Example 1 of mysql_connect_errno. ------------------------------------------------------------------------ [2020-12-17 19:42:12] requinix@php.net try { $mysqli = new mysqli('localhost', 'fake_user', 'my_password', 'my_db'); } catch (\Throwable $t) { echo $mysqli->connect_errno; } It's not a scope problem. It's a "if there was an exception thrown before/during the 'new mysqli' then the $mysqli variable will not be assigned" problem. ------------------------------------------------------------------------ [2020-12-17 19:20:37] bugs dot php dot net at latter dot org Definitely using the object. But we are trying to use the mysqli object inside an exception block. Seems to be some scope issues. ------------------------------------------------------------------------ [2020-12-17 18:39:53] cmb@php.net Apparently, you are confusing new mysqli() with mysqli_connect(). ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=80529 -- Edit this bug report at https://bugs.php.net/bug.php?id=80529&edit=1

« previous php.doc.bugs (#18284) next »