Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

500 Internal Server Error

Hey there. First time I'm using forum and as I didn't saw any post related to a similar problem... here's mine:

I'm starting "Using PHP with MySQL" track and right after the first lines of code I'm getting an 500 Internal Server Error and I can't get the object(PDO)#1 (0) {} message that Randy wants.

Basically... nope, not everything went correct. :P

I'm using WAMP therefore, I don't know if the proper code I should use is this one:

<?php
$db = new PDO("mysql:host=localhost;dbname=badbad","root"," ");
var_dump($db);
?>

I don't use password to enter my phpMyAdmin so, that's why I left password field in black. The path I'm using is something like:

localhost/htdocs/database.php

Thanks in advance for any tips around this and feel free to ask more information, cheers.

P.S.

"Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log."

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

Try removing the space between the quotation marks where your password is suppose to be in the new PDO declaration. Right now you are passing in a single space as the password when you want to be passing in nothing. So your PDO statement should look like this:

<?php
$db = new PDO("mysql:host=localhost;dbname=badbad", "root", "");
var_dump($db);
?>

Thanks for your answer Andrew, the password field was indeed wrong with that space between quotation marks. However, that was not even the main error. The problem was on the index file with the "include" and "ROOT_PATH" statement. As my path was localhost/htdocs/database.php instead of localhost/database.php, I had to change the code in the index file. At last but not the least, the file .htaccess was also giving problems. Cheers.