SQLSTATE[HY000][2002] php_network_getaddresses: ge

2019-04-24 11:44发布

问题:

My problem:


I use MAMP and Git to view and edit my PHP files with SQL database connection on my mac and then push it to the web server. I recently added a file directory. Here is the file with the SQl database connection:

<?php
ob_start();
session_start();

//set timezone
date_default_timezone_set('America/New_York');

//database credentials
define('DBHOST','mysql.hostinger.co.uk');
define('DBUSER','u536535282_evan7');
define('DBPASS','...');
define('DBNAME','u536535282_dbsql');

//application address
define('DIR','http://w-o-l.ml/');
define('SITEEMAIL','it@w-o-l.ml');

try {

    //create PDO connection
    $db = new PDO("mysql:host='.DBHOST.';port=8889;dbname='.DBNAME, DBUSER, DBPASS.'");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {
    //show error
    echo '<p>'.$e->getMessage().'</p>';
    exit;
}

//include the user class, pass in the database connection
include('classes/user.php');
$user = new User($db);

?>

But yet it returns the following error on the page:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

How do i fix it?


I cannot see my error so if someone could point it out, that would be helpful.

回答1:

Your quotes are all messed up.

$db = new PDO('mysql:host='.DBHOST.';port=8889;dbname='.DBNAME, DBUSER, DBPASS);


回答2:

I encountered the same Error with this line of code

$dsn = 'mysql:dbname=$dbname;host=$host;port=$port';

and i was able to resolve the error by replacing the single quotes with double quotes as below.

$dsn = "mysql:dbname=$dbname;host=$host;port=$port";

i spent two days looking for solution online and couldn't find it until i decided to do the above changes, and it worked. I still don't understand why it doesn't work with single quotes.



标签: php mysql mamp