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

时间:2014-02-18 20:47:49

标签: php

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.

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

第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());
    }
}

这就是设置数组的地方:

// 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'
    ) 
);

配置类:

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;
    }   
}

3 个答案:

答案 0 :(得分:2)

错别字:

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

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

答案 1 :(得分:0)

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

请检查! "mysql:host=".config::get('mysql/host')."之间是否有空格?因为太空而我得到了同样的错误!

答案 2 :(得分:0)

$ dbhost =“ localhost:3306”;添加MySQL端口对我有用