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

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

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.

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

<?php

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

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

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

well that will do it too :/

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 :(

I think i have solved it now!

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

<?php

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

?>

My Code

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