Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Using PHP with MySQL Connecting PHP to MySQL Getting Started with PDO

Can Not Connect To The Database?

I cannot connect to my mamp data base for this video. Here is my code:

<?php

$db = new PDO("mysql:host=localhost;dbname=shirts4mike;port=8889","root","root"); var_dump($db);

?>

It just loads a blank screen... HELP!

4 Answers

Check if your PDO extension is enabled in mamp.

Hi Trevor,

Put this at the beginning of your script and see what error messages you get.

ini_set('display_errors',1);
error_reporting(E_ALL);

Jeff

I don't get any error messages....

OK, now please don't be offended but I have to make sure. Is this how you're using the code?

<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

$db = new PDO("mysql:host=localhost;dbname=shirts4mike;port=8889","root","root"); 
var_dump($db);

?>

When I run this code I get messages like this:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in /home/jeff/public_html/treehouse/test/index.php:6 Stack trace: #0 /home/jeff/public_html/treehouse/test/index.php(6): PDO->__construct('mysql:host=loca...', 'root', 'root') #1 {main} thrown in /home/jeff/public_html/treehouse/test/index.php on line 6

Hey Jeff,

No offence taken. :) I copied and pasted your code exactly and still came up with a blank screen.... I know it's something simple I'm doing wrong, but for the life of me I can't figure it out...

I Got It!! Thanks Everyone!

"I got it!! Thanks Everyone!"

Well!! Give it to Us! What the hell was the problem.

Aaron Lafleur
Aaron Lafleur
10,474 Points

Hi Jeff,

I was experiencing the same issue that Trevor was - blank screen. I discovered I had inadvertently added a space between <? and php in the opening tag.... oops. Caused wayyy too much frustration. All is well now. Well, at least so far.

Off the top of my head have you double checked your port and password?

Also you can try a try/catch block to see what errors are happening.

<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

Also just a read a couple of places where people are having issues connecting over 8889. Once they switch the MAMP port over to 3306 and remove the 8889 from the connection string it hooks right up!... might give that a shot.

B Nawaz
B Nawaz
13,402 Points

So I had this same issue, and here's how I personally solved it:

Check which port is being used for MySQL in the MAMP Settings, and change the port in your database.php file to use this!