Remotely connecting to a MySQL database using PHP PDO

烈酒焚心 提交于 2021-02-05 11:24:06

问题


here i am trying to connect to MySQL database using PHP-PDO from remote server using IP address. when put ip address in place of host it gives me following error

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

this is the code

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

i have checked username and password they seems to be same as i gave.thank you in advance.


回答1:


You need to remove http from the host and put the port number under port attribute.

Please try with this line :

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


来源:https://stackoverflow.com/questions/34608260/remotely-connecting-to-a-mysql-database-using-php-pdo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!