完整错误是:
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();
}
答案 0 :(得分:0)
您在localhost中有一个空格。将配置更改为:
class Config
{
const DB_TYPE = 'mysql';
const DB_HOST = 'localhost'; // <- fixed this
const DB_USER = 'xxxx';
const DB_PASSWORD = 'xxxx';
const DB_NAME = 'xxxx';
}