gpt4 book ai didi

php - SQLSTATE[HY000] [2002] php_network_getaddresses : getaddrinfo failed

转载 作者:行者123 更新时间:2023-11-29 22:19:54 25 4
gpt4 key购买 nike

完整的错误是:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

这是我第一次尝试将网站与该主机连接。我不是一个大型 PHP 开发人员,我在这里看到的大多数代码与我所看到的有些不同。我可以连接到 phpmyadmin,但无法通过网站

class Config  
{
const DB_TYPE = 'mysql';
const DB_HOST = ' localhost';
const DB_USER = 'xxxx';
const DB_PASSWORD = 'xxxx';
const DB_NAME = 'xxxx';
}

class DatabasePDO {

/** @var PDO */
private static $dbh;
public static function getInstance()
{

$dsn = Config::DB_TYPE.':host='.Config::DB_HOST.';dbname='.Config::DB_NAME;

try {
self::$dbh = new PDO($dsn, Config::DB_USER, Config::DB_PASSWORD);
}
catch( PDOException $e ){
echo "Error!: ".$e->getMessage();
die();
}

return self::$dbh;
}
}


require_once WWW_ROOT. 'php' .DS. 'classes' .DS. 'Config.php';
require_once WWW_ROOT. 'php' .DS. 'classes' .DS. 'DatabasePDO.php';

class UserDAO {

public $dbh;

public function __construct()
{
$this->dbh = DatabasePDO::getInstance();
}

最佳答案

本地主机中有一个空格。将配置更改为:

class Config  
{
const DB_TYPE = 'mysql';
const DB_HOST = 'localhost'; // <- fixed this
const DB_USER = 'xxxx';
const DB_PASSWORD = 'xxxx';
const DB_NAME = 'xxxx';
}

关于php - SQLSTATE[HY000] [2002] php_network_getaddresses : getaddrinfo failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30835208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com