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

George Michael
George Michael
1,783 Points

Fatal error & PDOException [MySQL]

Hi ! I was going through the course of php then when i created the file connection.php file i tried to test it and i see the messages in title on line 3 of the same document. I use MySQL so that may be why but i pasted the code in teacher's notes so... i don't really get it

2 Answers

Carlos Alberto Del Cueto Carrejo
Carlos Alberto Del Cueto Carrejo
13,817 Points

If you only copy and paste the information from the workspace it could generate errors because of the configuration may change. I do use MySQL and my files look like this

<?php
require_once ('config.php');

try {
    $db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME .";port=" . DB_PORT,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;
} 
?>

and config.php looks like

<?php



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

    define("DB_HOST","localhost");
    define("DB_NAME","databaseName");
    define("DB_PORT","3306"); // default: 3306
    define("DB_USER","root");
    define("DB_PASS","myPassword");

Remember that you can change the password and create users for mysql. I changed the password of the user root. Hope this helps

George Michael
George Michael
1,783 Points

thank you :) but i must be missing something (i'm such a noob ><') your error message is correctly working though :P

so correct me if i'm wrong but i did not change anything to your connection.php file since it seems to be calling config.php so i changed config.php :

  • define("BASE_URL","localhost/treehouse1/"); //that's what i type in my url to access the files
  • define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"] . "/" . "treehouse1/"); // i don't really understand what this does but i'm guessing it's the same thing as above
  • define("DB_HOST","localhost"); // didn't change anything here
  • define("DB_NAME","treehouse"); //the name of the database i created in MySQL
  • i didn't change the port so it should by default "3306" and i didn't changed the User name either so it's root
  • define("DB_PASS",""); i don't have a password so i removed it here as well

P.S how do you change the design of your response so we can clearly differentiate when we are reading code ? (please don't be too harsh i'm a new member ? :P)

Carlos Alberto Del Cueto Carrejo
Carlos Alberto Del Cueto Carrejo
13,817 Points

The BASE_URL is , as you put it yourself, its what you type in the url to access your files or in other words the structure of your project, so let's say you move your project from one PC to another one by copying all files on the folder, since the BASE_URL is defined you will not need to update every file for the project to work, hence we don't save C:\home ... . ROOT_PATH is the root directory of your server, in this case your PC, again if you migrate to another server and you made reference to files outside of your project such as another program or library, this will also be carried to the new server, as long as its not that different of an OS. Everything else is alright, DB_HOST can change if you use a Database stored outside of your PC, and since you have not changed your MySQL user and password those fields are also Ok. About the design of the response, maybe it's just a lack of experience, for me it is very clear that the connection is being done correctly or it fails.
A try block tells PHP , hey PHP try to do this, and if it can't it catches the error, meaning you will not get a blanck page with an error message saying 404 or whatver code and instead it tells you, Could not connect to the database, this is useful because you will end up requiring that connection.php file in many other files, if for some reason it fails it will throw that message on the top of the screen, you will not have to check whether you have a bug on the page, or maybe some syntax error since you should pinpoint that error and relation it to the database.