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 Integrating PHP with Databases Databases and PHP Getting to Know the Project

Problem using MAMP: "Connection failed: Can't connect to MySQL server "

I have a php file that tries to connect to the MySQL server in MAMP, but I get an error message when I try to connect:

Connection failed: Can't connect to MySQL server

Here's the code I use to log in:

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "learnukr_membership";

// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

1 Answer

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hi John Gu,

It's really difficult to say what the problem is, it might be something with ports if other applications use port 80 for example like Skype (You could try to Quit Skype and try again). But I noticed in your code that user is root and password is root. With default instaliation of MAMP/WAMP or XAMP there is ussually an empty password. Did you set your password in MySQL? It might sound a silly question but is your Apache Web Server running?