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 Started with PDO

php connect to mysql on mamp

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

i have already checked that the host, username, and passport are all right. and the dbname I'm not sure.( i download the project file here : https://goo.gl/acQNPk)

please help me to solve the problem, thank you very much ! already try these methods on treehouse community, still can't work: https://goo.gl/efiZkD、https://goo.gl/jKt2hv、https://goo.gl/efiZkD

4 Answers

Jonathan Prada
Jonathan Prada
7,039 Points

After importing the database to MySql. The below works On MAMP with MySQL.

  • Inside your connection.php copy the following:

<?php //new PHP Data Object //This allows PHP to interact with a database $dsn = "mysql:host=localhost;dbname=database;port=8889"; $username = "root"; $password = "root"; //(For local use, PHPMyAdmin doesn't use a password.) $db = new PDO($dsn, $username, $password); //if this dumps an object we are connected var_dump($db); ?>

  • Go the url address where your connection.php file is:

Example: http://localhost:8888/mediasite/inc/connection.php

  • If you see an object like the one below, it means you are ready to continue the tutorial,

object(PDO)#1 (0) { }

Mike Henry
PLUS
Mike Henry
Courses Plus Student 5,373 Points

While I was doing the course I used sqlite that was in the download files this is it $db = new PDO("sqlite:".DIR."/database.db"); (In the connection.php file.)

After I finished the course I switched it to MySQL and use this $db = new PDO('mysql:host=127.0.0.1;port=3306;dbname=tth_php_database', 'root', ''); I also had to upload the database into MySQL by running HeidiSQL and doing a -load SQL file (database.sql) and then run SQL file. There were errors. I had to go to the SQL file and change line # 358, in SQLite it is allowed to have a "error" in the schema - but MySQL errors on the line INSERT INTO Media_People VALUES (26,"",'star'); so I changed it to INSERT INTO Media_People VALUES (26,163,'star'), I also changed the database name to tth_php_database ; I'm on a windows machine so I use WAMP.

I hope this gets you close enough to get it to work.

I don't use MAMP, I use WAMP. But... If anyone using WAMP has a similar issue, this worked well:

  1. Download the "database" SQL file in the project files and place it in the "www" directory in the "wamp" main directory on your disk (or elsewhere, so long as you know where it is).
  2. Load up PHPMyAdmin (http://localhost/phpmyadmin/ in your browser or click on PHPMyAdmin in the WAMPServer menu) and import the database by clicking "import" at the top of the page and browsing to the location in step 1.
  3. $dsn = "mysql:host=localhost;dbname=database;port=3306";

    $username = "root";

    $password = ""; //(For local use, PHPMyAdmin doesn't use a password.)

    $db = new PDO($dsn, $username, $password);

I'm having the same issue.