Why do I still execute the else statement after a connection error? Solution

Source: Internet
Author: User
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 ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.