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

From: Date: Thu, 17 Dec 2020 19:42:12 +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-18281@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: 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. Previous Comments: ------------------------------------------------------------------------ [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 (#18281) next »