doujiang9887 2017-10-24 18:35
浏览 83
已采纳

无法处理'getaddrinfo failed'

I have a PHP application which connects to a database.

The connection details (hostname,username,password,etc.) are supplied by the user. The problem is that when the user enters a non-exsisting hostname, I get the following warning:

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed

How do I handle this error? I am already using try-catch and it handles other exceptions perfectly (wrong username or password), but not this one.

Here is my code:

backend.php

<?php
// Library setup
require_once "instlib.php";
$lib = new installer;

// Header
header('Content-Type: application/json; Charset=UTF-8');

try {
    $lib->create_mysqli(array(
        "host" => "a",
        "user" => "b",
        "pass" => "c",
        "database" => "",
        "port" => "3306"
    ));
    echo $lib->build_response("AWESOME!", true);
} catch (Exception $e) {
    echo $lib->build_response($e->getMessage(), false);
}
?>

instlib.php

<?php
require_once 'library.php';
class installer extends nncms
{   
    public function build_response($response = "", $success, $extra = array())
    {
        return json_encode(array_merge(array('success' => $success, 'response' => $response),$extra));
    }
}
?>

library.php

<?php
class nncms
{   
    var $mysqli;

    public function create_mysqli($config)
    {
        // Set MySQLi to throw expection instead of warning
        mysqli_report(MYSQLI_REPORT_STRICT);

        // Connection setup
        $mysqli = new mysqli(
            $config["host"],
            $config["user"],
            $config["pass"],
            $config["database"],
            $config["port"]
        );
        $mysqli->set_charset('utf8mb4');

        // In case of an error that somehow didn't throw an exception
        if ($mysqli->connect_errno)
            throw new Exception("Connection error: ".$mysqli->connect_error);

        // Set MySQLi object of class
        $this->mysqli = $mysqli;
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dq804806 2017-10-24 18:53
    关注

    To change the behavior of PHP when there's a Warning, there's several options.

    First option, use the error suppressor operator @. You don't want to do this. It "works", but it will bring you problems down the road when you can't find the reason for a bug you're having.

    if ($db = @mysqli_connect("a", "a", "a", "a")) {
        // connected
    }
    

    The better option is to use set_error_handler() to throw an ErrorException whenever you run into a Warning. This will let you handle errors gracefully as Exceptions:

    function errorHandler($errno, $errstr, $errfile, $errline) {
        throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
    }
    set_error_handler("errorHandler");
    try {
        if ($db = mysqli_connect("a", "a", "a", "a")) {
            // connected
        }
    }
    catch(ErrorException $e) {
        echo "Exception: ".$e->getMessage();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)