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

时间:2015-02-15 04:15:33

标签: php mysql pdo

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

  

警告:PDO :: __ construct():php_network_getaddresses:getaddrinfo   失败:没有这样的主人知道。在   C:\ XAMPP \ htdocs中\ \畏惧最近\测试\ Feedstack主\ Feedstack主\程序\型号\ install.php了   第35行

     

数据库错误:SQLSTATE [HY000] [2002] php_network_getaddresses:getaddrinfo   失败:没有这样的主人知道。

     

第35行// $ db =新PDO(" mysql:host = $ host; db_name = $ db_name",$ user,   $通);

任何帮助!

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

3 个答案:

答案 0 :(得分:0)

就像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);

答案 1 :(得分:0)

这是我如何解决它

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

答案 2 :(得分:-3)

尝试通过MYSQLI连接

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