0

I'm trying to connect to my mySQL database using the PDO class in PHP.

Here is my Code :

// Connects to Our Database via PDO. if($local) { try { $db = new PDO("mysql:=localhost;dbname=bbc_archive;port=3306", "root", ""); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $db->exec("SET NAMES 'utf8'"); } catch(Exception $e) { echo "Connection to the DataBase was not possible. "; die(); } } else { try { $db = new PDO("mysql:=bbcarchive.db.11505263.hostedresource.com;dbname=bbcarchive", "bbcarchive", "myPassword"); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $db->exec("SET NAMES 'utf8'"); } catch(Exception $e) { echo "Connection to the live DataBase was not possible. "; die(); } } 

The $local variable is defined before and determines whether or not the script is running on a the live server or a test server.

When running in my local environment everthing works fine but on my live server it echo's out "Connection to the live DataBase was not possible." from the catch block.

I've contacted my host provider (godaddy) and they think it's a coding error. I've also, obviously, checked the hostname, dbname, username and password a 100 times and it's all correct. I just can't see the problem!

How can i do this ?

2
  • In your seconde catch block, add this code and show us the output: echo $e->getMessage(); Commented Mar 19, 2014 at 10:18
  • In your second try{} your DB_HOST should be localhost as well. Commented Mar 19, 2014 at 10:19

1 Answer 1

1

Your DSN seems to be incorrect. The documentation on MySQL DSNs indicates that it should look somewhat like this:

$db = new PDO("mysql:host=localhost;dbname=bbc_archive;port=3306", "root", ""); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It's always the little details that you can be blind to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.