SQLSTATE [HY000] [2002]和PDO :: __ construct()

时间:2015-07-16 14:55:14

标签: php mysql pdo connection

我一直在尝试与pdo建立联系并不断给出错误。

我的代码是

try {

$db=new PDO("mysql:host=localhost".DBHOST.";port=3306;proflab=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {
    //show error
    echo '<p class="bg-danger">'.$e->getMessage().'</p>';
    exit;
}

和错误是

警告:PDO :: __ construct():在第21行的C:\ xampp \ htdocs \ loginregister-master \ includes \ config.php

SQLSTATE [HY000] [2002] php_network_getaddresses:getaddrinfo failed:Esteanfitriãonãoéconhecido。

3 个答案:

答案 0 :(得分:2)

$db=new PDO("mysql:host=localhost".DBHOST.";port=3306;proflab=".DBNAME, DBUSER, DBPASS);
                                ^^^^^^^^

您正在生成localhostFOO,但不存在......

答案 1 :(得分:1)

连接字符串不正确:

$db=new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);

答案 2 :(得分:0)

不应该

host=localhost".DBHOST."

host=".DBHOST."

没有“localhost”?