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

error trying to connect to MySQL with MAMP using PHP using MAMP defaults

i have this big issue thats going on several days now, i am having difficulty connecting to localhost

my mamp server says mysql and apache are running and my mysql port is the the default 8889, i have left the default login and password as root

i can successfully connect to the database through the MAMP open webstart page and navigate to the phpmyadmin page successfully. I am able to add accounts, add a database through phpmyadmin GUI and do everything needed through phpmyadmin, however i CAN NOT seem to get connected through PHP.

the current error in chrome i am receiving is

"No data received ERR_EMPTY_RESPONSE Unable to load the webpage because the server sent no data."

i have a php file located in Applications>MAMP>htdocs>database.php with the following code:

<?php $db = new PDO("mysql:host=localhost;dbname=shirts4mike;port=8889","root","root"); var_dump($db);

?> i have tried uninstalling and reinstalling MAMP and it feels like a lost cause. Any help is appreciated.

-Aron

1 Answer

Lucas Santos
Lucas Santos
19,315 Points

I can't see all of your code and the part that you did paste in isn't formatted correctly but here is how I connect to a database through a local host.

<?php 

$servername = "localhost";
$username = "root";
$password = "root";
$port = 8889;
$myDB = "mydb";

try{
$conn = new PDO("mysql:host=$servername; dbname=$myDB; port=$port", $username, $password);
 // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->exec("SET NAMES 'utf8'");

}catch(Exception $e){
      echo "Error: " . $e->getMessage();
      exit;
    }
?>

thank you for your response, unfortunately I still get the same error :(

"No data received

ERR_EMPTY_RESPONSE Unable to load the webpage because the server sent no data."