Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?