$db = new PDO("mysql:host=$host;db_name=$db_name", $user, $pass);

$db = new PDO("mysql:host=$host;db_name=$db_name", $user, $pass);

我在安装脚本时遇到了这个问题。它给了我以下错误:

Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\dreads\recent\test\Feedstack-master\Feedstack-master\app\models\install.php on line 35

DB ERROR: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.

line 35 //$db = new PDO("mysql:host=$host;db_name=$db_name", $user, $pass);

任何帮助!

defined('_PATHANG') or die;

class modelInstall{

    public function main($request){

            $db_name = $request->get('db_name');
            $host = $request->get('host');
            $user =$request->get('db_username');
            $pass = $request->get('db_password');


            $site_name = $request->get('site_name');
            $site_url = $request->Get('site_url');

            $this->create_db($db_name,$host,$user,$pass);
            $this->create_tables($db_name,$host,$user,$pass);
            $this->update_config($site_name,$site_url,$db_name,$host,$user,$pass);

    }

    public function create_db($db_name,$host,$user,$pass)
    {
            try {
            $db = new PDO("mysql:host=$host;db_name=$db_name", $user, $pass);
                $db->exec("CREATE DATABASE `$db_name`;") 
                or die(print_r($db->errorInfo(), true));
            } catch (PDOException $e) {
                die("DB ERROR: ". $e->getMessage());
            }   
    }

尝试通过 MYSQLI 连接

    $con = mysqli_connect("localhost","my_user","my_password","my_db");

正如 Hobo Sapiens 所说,您提供的主机不存在或拼写错误,我不知道您的 $request 对象的结构。您的对象结构需要如下所示。

class Request 
{
private $host;
private $db_name;
private $db_username;
private $db_pwd;

public function __construct($host,$db_name,$db_username,$db_pwd) {
    $this->host = $host;
    $this->db_name = $db_name;
    $this->db_username = $db_username;
    $this->db_pwd = $db_pwd;
}
public function getHost(){
    return $this->host;
}
public function getDBName(){
    return $this->db_name;
}
public function getDBUsername(){
    return $this->db_username;
}
public function getDBPwd(){
    return $this->db_pwd;
}
}

然后你可以做类似下面的事情

$request = new Request('localhost', 'TestingPDO', 'root', '');
$modelinstall = new modelInstall();
$modelinstall->main($request);

我就是这样解决的

<?php

defined('_PATHANG') or die;
class modelInstall{
    public function main($request){
            $db_name = $request->get('db_name');
            $host = $request->get('host');
            $user =$request->get('db_username');
            $pass = $request->get('db_password');


            $site_name = $request->get('site_name');
            $site_url = $request->Get('site_url');
            $this->create_db($db_name,$host,$user,$pass);
            $this->create_tables($db_name,$host,$user,$pass);
            $this->update_config($site_name,$site_url,$db_name,$host,$user,$pass);
    }
    public function create_db($db_name,$host,$user,$pass)
    {
            try {
                $db = new PDO("mysql:host=localhost", 'root', 'mypassword');

                $db->exec("CREATE DATABASE `feedstack`;") 
                or die(print_r($db->errorInfo(), true));
            } catch (PDOException $e) {
                die("DB ERROR: ". $e->getMessage());
            }   
    }