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 CRUD Operations with PHP Project Setup Connecting to the Database

Amit Kumar
Amit Kumar
1,063 Points

how to connect mysql on local environment

$db = new PDO("mysql:host=localhost;dbname=database.bd;port=3306","root","mypass");

2 Answers

If you're using MAMP the default password and username are both root and also you don't have to type the file type on the dbname part. So for me the following statement worked well

$db = new PDO("mysql:host=localhost;dbname=database;port=3306","root","root");

Also before doing all that you should've imported your database into mysql

Your connection looks correct except there is a typo in your dbname. Try changing database.bd to database.db.

Make sure that your are connected to your local Server (Such as XAMPP or WAMP etc.), that the server is running and that the database exists on the server otherwise there is nothing to connect to and it will fail.