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

sebastian rojas
sebastian rojas
571 Points

Can't connect to database - mysql

Hey!

I am following this video http://teamtreehouse.com/library/using-php-with-mysql/connecting-php-to-mysql/getting-started-with-pdo-2

and i can't connect to the database! I have checked so my code is right and such but it won't work at all!

Anyone can help me with this?

Kind regards, Sebastian

Andrew McCormick
Andrew McCormick
17,730 Points

please post your code that you have so far. thanks.

6 Answers

Try removing the last parameter (password). XAMMP has the username of root, but no password.

David Rynn
David Rynn
10,554 Points

Removing the last parameter, the password ('root') worked for me too. Thanks.

sebastian rojas
sebastian rojas
571 Points
<?php

$db = new PDO("mysql:host=localhost;dbname=sweaters4simba;port=8889", "root", "root");
var_dump($db);
Andrew McCormick
Andrew McCormick
17,730 Points

have you tried without the port? most of the time (at least in my experience) you don't need to specify the post.

sebastian rojas
sebastian rojas
571 Points

Ah, no i havent! but now i have some big problems with xampp haha..

sebastian rojas
sebastian rojas
571 Points

So i removed the port

<?php

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


?>

and got this error

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in C:\xampp\htdocs\database.php:3 Stack trace: #0 C:\xampp\htdocs\database.php(3): PDO->__construct('mysql:host=loca...', 'root', 'root') #1 {main} thrown in C:\xampp\htdocs\database.php on line 3

trying to understand the problem but don't :(

sebastian rojas
sebastian rojas
571 Points

I think i have solved it now!

Atiba Lashley
Atiba Lashley
5,724 Points

I'm havin the same problem....when try to load it I get this message:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in C:\xampp\htdocs\database.php:3 Stack trace: #0 C:\xampp\htdocs\database.php(3): PDO->__construct('mysql:host=loca...', 'root', 'root') #1 {main} thrown in C:\xampp\htdocs\database.php on line 3

Atiba Lashley
Atiba Lashley
5,724 Points

<?php

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

?>

My Code

Muhammad Atif
Muhammad Atif
117 Points

I was having the same issue .Removing the last parameter(password) worked for me .Thanks Ivan