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

Mysql and PHP

config.php:

<?php

// these two constants are used to create root-relative web addresses
// and absolute server paths throughout all the code

define("BASE_URL","/");
define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"] . "/");

define("DB_HOST","moviecrewhunter.com.mysql");
define("DB_NAME","products");
define("DB_USER","movikcrewhunter");
define("DB_PASS","xcx<cxx<zc<zx");

try { $db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME ."; . DB_USER,DB_PASS); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $db->exec("SET NAMES 'utf8'"); } catch (Exception $e) { echo "Could not connect to the database."; exit; }

So why I cant get it to work????

3 Answers

Not working

when adding include("inc/config.php"); I could get it to write could not connect to the database.

But now it looks wierd, and I havnt change anything with layout :(

http://moviecrewhunter.com/shirts/

found out, some how the have delete code in products.php without showing in the video.

Hi Claus,

Are you sure moviecrewhunter.com.mysql is the hostname for your MySQL server as to me that looks wrong, also posting your password online isn't a wise choice so I recommend you remove it.

Yes I got host from one.com, and password is just fake

You had a couple of syntax errors in your code after DB_NAME .", the semicolon and period shouldn't have been there but instead a comma should have been there.

Also it's good during development to have as much debugging code as you can while building up your code base to ensure you can find oversights and logic flaws.

Give the below a try

<?php

try {
    $db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
    echo "Could not connect to the database.<br><br>" . $e->getMessage();
    exit;
}

try { $db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME ."; . DB_USER,DB_PASS); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $db->exec("SET NAMES 'utf8'"); } catch (Exception $e) { echo "Could not connect to the database."; exit; }