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

Jun XIE
Jun XIE
10,363 Points

can not connect to the database and shows access forbidden

In the video"getting started with PDO", I typed "<?php $db = new PDO("mysql:host=localhost;dbname=shirts4mike;port=3306","root",""); var_dump($db); ?> as it instructs. I opened the database.php file in the browser, but it shows "Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server.". Then I checked in terminal that the port is 3306 and the password is blank, they are no problem. I also can access shirts4mike in terminal. I don't know if someone has met the same situation. If so, could you please help me with it? :P. Thanks in advance!

1 Answer

Philip Cox
Philip Cox
14,818 Points

Hi Jun. As Randy mentions, you may not need to add the port number, I didn't. Try some variations, drop the port number and just use the user name and password, root, root. Also, you have MAMP or WAMP running I take it?

Jun XIE
Jun XIE
10,363 Points

Dear Cox, thanks for your help, ^_^. I am using XAMPP. I downloaded the code into htdocs directly as I didn't have my own codes. Just now I found the code file in terminal has different prefix than others. It is not "drwxr-xr-x". I tried to change the authority granted to me to "drwxr-xr-x". It works fine now.