Why do I still execute the else statement after a connection error? PHPcode & lt ;? Php $ mysqli = newmysqli ('localhost', 'root', 'root1', 'mydb'); if ($ mysqli-& gt; connect_error) {Why is the else statement executed after a connection error?
PHP code
Connect_error) {die ("connection failed". $ mysqli-> connect_error) ;}else {echo "connection successful" ;};?>
In the above code, I intentionally wrote root as root1. if it is not modified, the page displays the connection successful, but the connection error page displays the following
Warning: mysqli () [mysqli. mysqli]: (28000/1045): Access denied for user 'root' @ 'localhost' (using password: YES) in C: \ wamp \ www \ Project1 \ f. php on line 2
Warning: main () [function. main]: Couldn't fetch mysqli in C: \ wamp \ www \ Project1 \ f. php on line 4
Connection successful
Why is the connection error? the connection failure information in the die is not displayed?
If the connection fails, why is the connection in else still displayed successfully?
------ Solution --------------------
Simple
$ Mysqli is invalid when the connection fails.
If ($ mysqli-> connect_error)
Because $ mysqli is not an object, it cannot enter the true branch.
Therefore, only echo "connection successful" is executed ";