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
Claus Meyer
Courses Plus Student 2,523 PointsMysql 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
Claus Meyer
Courses Plus Student 2,523 PointsNot 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.
Chris Shaw
26,676 PointsHi 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.
Claus Meyer
Courses Plus Student 2,523 PointsYes I got host from one.com, and password is just fake
Chris Shaw
26,676 PointsYou 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;
}
Claus Meyer
Courses Plus Student 2,523 Pointstry { $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; }