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 trialPrakhar Patwa
11,260 Pointsgetting error to conect to database
<?php
$db = new pdo("mysql:host=localhost;dbname=shirts4mike;port=8889","root","root");
var_dump($db);
?>
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. ' in C:\xampp\htdocs\database.php:2 Stack trace: #0 C:\xampp\htdocs\database.php(2): PDO->__construct('mysql:host=loca...', 'root', '') #1 {main} thrown in C:\xampp\htdocs\database.php on line 2
1 Answer
Eric Butler
33,512 PointsThis just means there's something about your connection that is set wrong, or your MySQL server isn't running. If you didn't create the "shirts4mike" database ahead of time, you need to. But my guess would be that you're specifying the wrong port -- 8889 is not a standard port to use for MySQL; 3306 is. Try just removing the port setting altogether, it isn't strictly necessary:
<?php
$db = new PDO("mysql:host=localhost;dbname=shirts4mike", "root", "root");
var_dump($db);
?>
You also should probably capitalize PDO.
Here's links to the official documentation and a pretty thorough how-to on PDO connections and queries.
Good luck!