php SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 没有这样的主机是已知的。PHP错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21864957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:29:12  来源:igfitidea点击:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. PHP error

php

提问by Carefree4

Notice: Array to string conversion in C:\xampp\xampp\htdocs\classes\DB.php on line 21

Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\xampp\htdocs\classes\DB.php on line 21
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.

It gives me this error when its on XAMPP, but when i put it on a live server it works fine, I know the DB credentials are correct, I am using the current version of XAMPP (as of yesterday) for windows and using Apache 2 on the live server, both with MySql.

当它在 XAMPP 上时它给了我这个错误,但是当我把它放在实时服务器上时它工作正常,我知道数据库凭据是正确的,我正在使用当前版本的 XAMPP(截至昨天)Windows 并使用 Apache 2在实时服务器上,都带有 MySql。

Line 18-25:

第 18-25 行:

// Takes values from 'config.php' and uses them to connect
private function __construct() {
    try {
        $this->_pdo = new PDO('mysql:host=' . Config::get('msql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
    } catch(PDOException $e) {
        die($e->getMessage());
    }
}

And this is where the array is set:

这是设置数组的地方:

// Sets the config valuses
$GLOBALS['config'] = array(
    'mysql' => array(
        'host' => '127.0.0.1',
        'username' => 'root',
        'password' => 'password',
        'db' => 'site'
    ), 
    'remember' => array(
        'cookie_name' => 'hash',
        'cookie_expiry' => 604800
    ), 
    'session' => array(
        'session_name' => 'user'
    ) 
);

And the config class:

和配置类:

class Config {
    public static function get($path = null) {
        if($path) {
            $config = $GLOBALS['config'];
            $path = explode('/', $path);

            foreach($path as $bit) {
                if(isset($config[$bit])) {
                    $config = $config[$bit];
                }
            }

            return $config;
        }

        return false;
    }   
}

回答by Marc B

Typos:

错别字:

    $this->_pdo = new PDO('mysql:host=' . Config::get('msql/host') . ';dbname=' . 
                                                       ^^^^^---no Y

$GLOBALS['config'] = array(
    'mysql' => array(
      ^---has a Y

回答by fariha

$this->_pdo = new PDO("mysql:host=".config::get('mysql/host').";dbname=".config::get('mysql/database') ,config::get('mysql/user'),config::get('mysql/password'));

Please check! Is there any space between "mysql:host=".config::get('mysql/host')."? I was getting the same error because of space!

请检查!之间有空格"mysql:host=".config::get('mysql/host')."吗?由于空间原因,我遇到了同样的错误!