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

Could not connect to Database.

I am not able to connect through this PDO. But i my database is connected when i used mysql_connect. What is the difference between these two and why i am not able to connect to DB?

4 Answers

Kennard McGill
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Kennard McGill
Full Stack JavaScript Techdegree Graduate 43,179 Points

You should post the code you are using to connect to the DB so someone can help you troubleshoot your code.

For reference here's a good article on why you might want to use PDO PHP Data objects. Why use PDO?

I have read that but it will be of great help if you could tell me the difference between mysql_connect and PDO ?

Kennard McGill
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Kennard McGill
Full Stack JavaScript Techdegree Graduate 43,179 Points

You shouldn't use mysql_connect anymore for security reasons (sql injections). It's also been deprecated You should look at mysqli_* if you are more comfortable with using the old mysql_ statements

PDO allows you to connect to different database with one single interface as well as provide a bunch of other methods for working with data. Just need to check out the documentation for more info.

It is my understanding that PDO is a built in class, and mysql_connect is a built in function. It is also my understanding that one would create a class and use mysql_connect as methods. PDO is already a class so a lot of work has been programmed for us.

As for you connection, lets see the code !