|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-12-17 18:18 UTC] 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); } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 02:00:01 2025 UTC |
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.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.Makes sense. I think one of the things that got me confused was: try { $mysqli = new mysqli('localhost', 'fake_user', 'my_password', 'my_db'); } catch (\Throwable $t) { echo $mysqli->connect_errno; } If the constructor throws the assignment doesn't happen and there's still the same error. As bugs clarified, $t->getMessage() ought to be used in the catch. _And then_ look at connect_error to see if the DBMS refused the connection. Or convert errors to exceptions, and try looking at connect_error.