Can't get PHP PDO to work

software development

#1

Hey fellas!

I recently reactivated my (Shared Hosting) DreamHost account, and I’m trying to develop a relatively simple PHP program that needs to retrieve data from a mysql database. However, I keep getting this error:

ERROR: SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

My code is as follows:

 $this->database = new PDO(
                "mysql:host=" . $host . ";dbname=" . $database,
                $dbUser,
                $dbPassword);

I’ve used the values of $host, $dbUser, and $dbPassword to log in to myPHPAdmin, so I know they’re right. I’ve also double-checked the database name. Any idea what I’m doing wrong?


#2

The error message you’re getting indicates to me that PHP is either not seeing a hostname, or is ignoring it for some reason. Are you certain $host is set correctly in this context? (Try printing it right before you try connecting or something.)


#3

I’d probably change $host to $dbhost in case something trips that variable up later, too. Or maybe define the db stuff.

[php]define(“DBHOST”, “blah.domain.com”);
define(“DBNAME”, “blah”);
// more defines…
$this->database = new PDO(‘mysql:host=’.DBHOST.’;dbname=’.DBNAME.’’, DBUSER, DBPASS);[/php]