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

From: Date: Fri, 18 Dec 2020 00:09:38 +0000
Subject: Doc #80529 [Com]: $mysqli->connect_errno may not work
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18283@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 Comment by: a at b dot c dot de 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: 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:"? Previous Comments: ------------------------------------------------------------------------ [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(). ------------------------------------------------------------------------ [2020-12-17 18:18:21] bugs dot php dot net at latter dot org Description: ------------ --- From manual page: https://php.net/mysqli.construct --- This example code is given (as below) but if mysqli() returns false (as it will if for example the specified database does not exist) then $mysqli->connect_errno will generate a PHP warning of "Trying to get property 'connect_errno' of non-object". $mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db'); if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_errno); } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80529&edit=1

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