使用PHP PDO远程连接到MySQL数据库

时间:2016-01-05 09:32:25

标签: php mysql pdo

这里我试图使用IP地址从远程服务器使用PHP-PDO连接到MySQL数据库。当把ip地址代替主机时,它会给我以下错误

Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\xampp\htdocs\oppInsights\database\Database.php on line 32

Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in D:\xampp\htdocs\oppInsights\database\Database.php:39 Stack trace: #0 D:\xampp\htdocs\oppInsights\database\Select.php(800): Database->Connection() #1 D:\xampp\htdocs\oppInsights\decision.php(19): Select->expiryContracts() #2 {main} thrown in D:\xampp\htdocs\oppInsights\database\Database.php on line 39

这是代码

<?php

class Database {
    public  $dbhost = "mysql:dbname=apt;host=http://10.75.225.171:3601";
    public  $dbuser = "tribhuvan";
    public  $dbpass = "123456";
    public  $dbname = "apt";
    public  $connection;
    public  $selectdb;
    public  $isConnected;
    public  $dbh;

    //$user = 'dbuser';
    //$password = 'dbpass';

    public function Connection()
    {
        try
        {   
             $this->dbh = new PDO($this->dbhost, $this->dbuser, $this->dbpass);
            // echo "true";
                return  $this->dbh;
        }
        catch(Exception $e)
        { 
                $this->isConnected = false;
                throw new Exception($e->getMessage());
        }
    }

    public function Disconnect()
    {
        $this->datab = null;
        $this->isConnected = false;
    }
}
?>

我已经检查了用户名和密码,他们似乎和我给的一样。请提前告诉你。

1 个答案:

答案 0 :(得分:1)

您需要从主机中删除http并将端口号放在端口属性下。

请尝试使用此行:

$dbhost = "mysql:host=10.75.225.171;port=3601;dbname=apt";