'PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: no such host known

I am new in PHP and when I am trying to connect my database with PHP it is showing error

Uncaught PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\sql_php\pdo.php:2 Stack trace: #0 C:\xampp\htdocs\sql_php\pdo.php(2): PDO->__construct('mysql:host=port...', 'Rohitash', 'rohitcodes') #1 {main} Next PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\sql_php\pdo.php:2 Stack trace: #0 C:\xampp\htdocs\sql_php\pdo.php(2): PDO->__construct('mysql:host=port...', 'Rohitash', 'rohitcodes') #1 {main} thrown in C:\xampp\htdocs\sql_php\pdo.php on line 2

what can be the possible causes for that.



Solution 1:[1]

check your host is live or not if live try to check this code to connect your database

$hostName = "localhost";
$dbName = "test";
$userName = "test";
$password = "test1";
try {
    $pdo = new PDO("mysql:host=$hostName;dbname=$dbName",$userName,$password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
    catch(PDOException $e)
    {
     echo "Connection failed: " . $e->getMessage();
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jignesh Joisar