2 Replies - 279 Views - Last Post: 11 November 2013 - 02:12 AM Rate Topic: -----

#1 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 620
  • View blog
  • Posts: 2,772
  • Joined: 03-December 12

PDO Connection error

Posted 11 November 2013 - 12:36 AM

I receive this error
ERROR: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known 


For my PDO connection function

class Connection
{
	//Set variables for PDO
	private $hostname ="mysql:host=HostingDBaddress.com ; dbname=databaseName; charset=utf8"; //hostname for PDO use
	//private $hostname = "db501650667.db.1and1.com";											//Use for depricated mysql_connect
	private $username = "User";
	private $dbname = "databaseName";
	private $password = "password";
	
	function con(){
	
		try {
			$pdo = new PDO($this->hostname, $this->username, $this->password);
			$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		} catch(PDOException $e) {
	    echo 'ERROR: ' . $e->getMessage();
		}
	}

/*
	function con(){
	
		//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
     echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}

$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
    die ('Can\'t select database: ' . mysql_error());
}
else {
    echo 'Database ' . $dbname . ' successfully selected!';
}

mysql_close($link);
	
	}
*/



I have already spoken to the hosting company and after they looked into they say it is a scripting error. However, even when I use the connection string they provide it errors out saying the socket can't be reached... Is the script wrong? I removed the actual information in the variables, but those are copied and pasted from phpAdmins info on the DB.

This post has been edited by astonecipher: 11 November 2013 - 12:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PDO Connection error

#2 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 181
  • Joined: 19-October 13

Re: PDO Connection error

Posted 11 November 2013 - 02:02 AM

Not sure but have you tested if removing the spaces around the semicolon makes a difference?
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 620
  • View blog
  • Posts: 2,772
  • Joined: 03-December 12

Re: PDO Connection error

Posted 11 November 2013 - 02:12 AM

I found out that this:
dbname=databaseName;
was the problem, there should not be a semi-colon there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1