'new mysqli(...);'是什么返回是否发生错误?

时间:2018-12-14 15:12:56

标签: php mysqli connection

我想将mysqli对象保存到变量中。如果发生错误,构造函数将返回什么?我的意思是说我传递了错误的login / pass / ip / dbname,我收到警告消息(如果启用了警告),但是变量中的值是什么?

window.addEventListener("scroll", function () {
  console.log("scroll");
}, true);

谢谢,迈克

发布脚本
在手册中,我找不到查询器。

2 个答案:

答案 0 :(得分:1)

@Funk Forty Niner指出的PHP文档中。

https://secure.php.net/manual/en/mysqli.construct.php

  

注意:

     仅限

OO语法:如果连接失败,则仍然返回对象。要检查连接是否失败,请使用mysqli_connect_error()函数或mysqli-> connect_error属性,如上例所示。

答案 1 :(得分:0)

它仍然会返回一个mysqli对象,以便您可以获取connect_error:

$conn = new mysqli("bad", "bad", "so bad", "too bad, what then?");
print_r($conn);

结果(前两行多次打印):

Warning: print_r(): Couldn't fetch mysqli in /path/to/test.php on line 4 PHP
Warning:  print_r(): Couldn't fetch mysqli in /path/to/test.php on line 4 

Warning: print_r(): Couldn't fetch mysqli in /path/to/test.php on line 4 (
    [affected_rows] =>
    [client_info] =>
    [client_version] => 50011
    [connect_errno] => 2002
    [connect_error] => php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
    [errno] =>
    [error] =>
    [error_list] =>
    [field_count] =>
    [host_info] =>
    [info] =>
    [insert_id] =>
    [server_info] =>
    [server_version] =>
    [stat] =>
    [sqlstate] =>
    [protocol_version] =>
    [thread_id] =>
    [warning_count] => )