SQLSTATE [HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename or servname provided or unknown

My problem:


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

<?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://wol.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 still, it returns the following error on the page:

SQLSTATE [HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename or servname provided or unknown

How to fix it?


I do not see my mistake, so if someone could point to it, that would be helpful.

+6
source share
2 answers

Your quotes are all messed up.

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

I ran into 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 single quotes with double quotes, as shown below.

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

I spent two days searching for a solution on the Internet and could not find it until I decided to make the above changes, and it worked. I still do not understand why it does not work with single quotes.

0
source

All Articles