Home Backend Development PHP Tutorial 连接出错后为什么还会执行else后的语句?解决办法

连接出错后为什么还会执行else后的语句?解决办法

Jun 13, 2016 am 10:20 AM
connect error mysqli quot root

连接出错后为什么还会执行else后的语句?

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $mysqli=new mysqli('localhost','root','root1','mydb');    if ($mysqli->connect_error){        die("连接失败".$mysqli-&gt;connect_error);    }else{        echo "连接成功";    };?&gt;
Copy after login

上面代码,我故意把root写成root1,如果不改,页面显示连接成功,可是连接出错页面会显示如下
Warning: mysqli::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
连接成功
为什么连接出错了,没有显示die里的 连接失败 信息?
如果连接失败,为什么还会显示else里的连接成功?

------解决方案--------------------
很简单
当连接失败后, $mysqli 就是无效的
if ($mysqli->connect_error)
就会因为 $mysqli 不是对象,而不能进入 true 分支

于是只会执行 echo "连接成功";
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Log in to Ubuntu as superuser Log in to Ubuntu as superuser Mar 20, 2024 am 10:55 AM

Log in to Ubuntu as superuser

Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Jun 22, 2023 pm 06:40 PM

Solution to PHP Fatal error: Call to undefined method PDO::prepare() in

Solve the problem of 'error: incomplete type is not allowed' in C++ code Solve the problem of 'error: incomplete type is not allowed' in C++ code Aug 26, 2023 pm 08:54 PM

Solve the problem of 'error: incomplete type is not allowed' in C++ code

What should I do if 'Uncaught (in promise) Error: Request failed with status code 500' occurs when using axios in a Vue application? What should I do if 'Uncaught (in promise) Error: Request failed with status code 500' occurs when using axios in a Vue application? Jun 24, 2023 pm 05:33 PM

What should I do if 'Uncaught (in promise) Error: Request failed with status code 500' occurs when using axios in a Vue application?

0271: What should I do if the computer cannot be turned on due to real time clock error? 0271: What should I do if the computer cannot be turned on due to real time clock error? Mar 13, 2023 am 11:30 AM

0271: What should I do if the computer cannot be turned on due to real time clock error?

Solve the 'error: expected initializer before 'datatype'' problem in C++ code Solve the 'error: expected initializer before 'datatype'' problem in C++ code Aug 25, 2023 pm 01:24 PM

Solve the 'error: expected initializer before 'datatype'' problem in C++ code

How to change the root user name in Linux How to change the root user name in Linux May 18, 2023 pm 07:50 PM

How to change the root user name in Linux

How to solve PHP Warning: fopen(): failed to open stream: No such file or directory How to solve PHP Warning: fopen(): failed to open stream: No such file or directory Aug 19, 2023 am 10:44 AM

How to solve PHP Warning: fopen(): failed to open stream: No such file or directory

See all articles